From 7b875d705eb5d79e60f9a6dec39bcb22e22c9b7a Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 28 Jul 2023 19:27:30 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.0469 No.0469.Convex Polygon --- .../0418.Sentence Screen Fitting/README.md | 2 +- .../0418.Sentence Screen Fitting/README_EN.md | 2 +- .../0400-0499/0469.Convex Polygon/README.md | 95 ++++++++++++++++++- .../0469.Convex Polygon/README_EN.md | 87 ++++++++++++++++- .../0469.Convex Polygon/Solution.cpp | 21 ++++ .../0400-0499/0469.Convex Polygon/Solution.go | 18 ++++ .../0469.Convex Polygon/Solution.java | 23 +++++ .../0400-0499/0469.Convex Polygon/Solution.py | 15 +++ 8 files changed, 259 insertions(+), 4 deletions(-) create mode 100644 solution/0400-0499/0469.Convex Polygon/Solution.cpp create mode 100644 solution/0400-0499/0469.Convex Polygon/Solution.go create mode 100644 solution/0400-0499/0469.Convex Polygon/Solution.java create mode 100644 solution/0400-0499/0469.Convex Polygon/Solution.py diff --git a/solution/0400-0499/0418.Sentence Screen Fitting/README.md b/solution/0400-0499/0418.Sentence Screen Fitting/README.md index 3c6b0cfab9c79..f54e633a0259a 100644 --- a/solution/0400-0499/0418.Sentence Screen Fitting/README.md +++ b/solution/0400-0499/0418.Sentence Screen Fitting/README.md @@ -144,7 +144,7 @@ class Solution { public: int wordsTyping(vector& sentence, int rows, int cols) { string s; - for (auto& t: sentence) { + for (auto& t : sentence) { s += t; s += " "; } diff --git a/solution/0400-0499/0418.Sentence Screen Fitting/README_EN.md b/solution/0400-0499/0418.Sentence Screen Fitting/README_EN.md index 60769f1e3543d..6825dad0f49ba 100644 --- a/solution/0400-0499/0418.Sentence Screen Fitting/README_EN.md +++ b/solution/0400-0499/0418.Sentence Screen Fitting/README_EN.md @@ -106,7 +106,7 @@ class Solution { public: int wordsTyping(vector& sentence, int rows, int cols) { string s; - for (auto& t: sentence) { + for (auto& t : sentence) { s += t; s += " "; } diff --git a/solution/0400-0499/0469.Convex Polygon/README.md b/solution/0400-0499/0469.Convex Polygon/README.md index 7d45a19bfbfd5..37a9f1f3648c9 100644 --- a/solution/0400-0499/0469.Convex Polygon/README.md +++ b/solution/0400-0499/0469.Convex Polygon/README.md @@ -47,6 +47,14 @@ +**方法一:数学(向量叉积)** + +假设当前连续的三个顶点分别为 $p_1, p_2, p_3$,我们可以计算向量 $\overrightarrow{p_1p_2}$ 和 $\overrightarrow{p_1p_3}$ 的叉积,记为 $cur$。如果 $cur$ 的方向与之前的 $pre$ 方向不一致,说明多边形不是凸多边形。否则,我们更新 $pre = cur$,继续遍历下一个顶点。 + +遍历结束,如果没有发现不一致的情况,说明多边形是凸多边形。 + +时间复杂度 $O(n)$,其中 $n$ 是顶点的数量。空间复杂度 $O(1)$。 + ### **Python3** @@ -54,7 +62,21 @@ ```python - +class Solution: + def isConvex(self, points: List[List[int]]) -> bool: + n = len(points) + pre = cur = 0 + for i in range(n): + x1 = points[(i + 1) % n][0] - points[i][0] + y1 = points[(i + 1) % n][1] - points[i][1] + x2 = points[(i + 2) % n][0] - points[i][0] + y2 = points[(i + 2) % n][1] - points[i][1] + cur = x1 * y2 - x2 * y1 + if cur != 0: + if cur * pre < 0: + return False + pre = cur + return True ``` ### **Java** @@ -62,7 +84,78 @@ ```java +class Solution { + public boolean isConvex(List> points) { + int n = points.size(); + long pre = 0, cur = 0; + for (int i = 0; i < n; ++i) { + var p1 = points.get(i); + var p2 = points.get((i + 1) % n); + var p3 = points.get((i + 2) % n); + int x1 = p2.get(0) - p1.get(0); + int y1 = p2.get(1) - p1.get(1); + int x2 = p3.get(0) - p1.get(0); + int y2 = p3.get(1) - p1.get(1); + cur = x1 * y2 - x2 * y1; + if (cur != 0) { + if (cur * pre < 0) { + return false; + } + pre = cur; + } + } + return true; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + bool isConvex(vector>& points) { + int n = points.size(); + long long pre = 0, cur = 0; + for (int i = 0; i < n; ++i) { + int x1 = points[(i + 1) % n][0] - points[i][0]; + int y1 = points[(i + 1) % n][1] - points[i][1]; + int x2 = points[(i + 2) % n][0] - points[i][0]; + int y2 = points[(i + 2) % n][1] - points[i][1]; + cur = 1L * x1 * y2 - x2 * y1; + if (cur != 0) { + if (cur * pre < 0) { + return false; + } + pre = cur; + } + } + return true; + } +}; +``` +### **Go** + +```go +func isConvex(points [][]int) bool { + n := len(points) + pre, cur := 0, 0 + for i := range points { + x1 := points[(i+1)%n][0] - points[i][0] + y1 := points[(i+1)%n][1] - points[i][1] + x2 := points[(i+2)%n][0] - points[i][0] + y2 := points[(i+2)%n][1] - points[i][1] + cur = x1*y2 - x2*y1 + if cur != 0 { + if cur*pre < 0 { + return false + } + pre = cur + } + } + return true +} ``` ### **...** diff --git a/solution/0400-0499/0469.Convex Polygon/README_EN.md b/solution/0400-0499/0469.Convex Polygon/README_EN.md index f6aea976a25d7..ce00e6395ae19 100644 --- a/solution/0400-0499/0469.Convex Polygon/README_EN.md +++ b/solution/0400-0499/0469.Convex Polygon/README_EN.md @@ -42,13 +42,98 @@ ### **Python3** ```python - +class Solution: + def isConvex(self, points: List[List[int]]) -> bool: + n = len(points) + pre = cur = 0 + for i in range(n): + x1 = points[(i + 1) % n][0] - points[i][0] + y1 = points[(i + 1) % n][1] - points[i][1] + x2 = points[(i + 2) % n][0] - points[i][0] + y2 = points[(i + 2) % n][1] - points[i][1] + cur = x1 * y2 - x2 * y1 + if cur != 0: + if cur * pre < 0: + return False + pre = cur + return True ``` ### **Java** ```java +class Solution { + public boolean isConvex(List> points) { + int n = points.size(); + long pre = 0, cur = 0; + for (int i = 0; i < n; ++i) { + var p1 = points.get(i); + var p2 = points.get((i + 1) % n); + var p3 = points.get((i + 2) % n); + int x1 = p2.get(0) - p1.get(0); + int y1 = p2.get(1) - p1.get(1); + int x2 = p3.get(0) - p1.get(0); + int y2 = p3.get(1) - p1.get(1); + cur = x1 * y2 - x2 * y1; + if (cur != 0) { + if (cur * pre < 0) { + return false; + } + pre = cur; + } + } + return true; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + bool isConvex(vector>& points) { + int n = points.size(); + long long pre = 0, cur = 0; + for (int i = 0; i < n; ++i) { + int x1 = points[(i + 1) % n][0] - points[i][0]; + int y1 = points[(i + 1) % n][1] - points[i][1]; + int x2 = points[(i + 2) % n][0] - points[i][0]; + int y2 = points[(i + 2) % n][1] - points[i][1]; + cur = 1L * x1 * y2 - x2 * y1; + if (cur != 0) { + if (cur * pre < 0) { + return false; + } + pre = cur; + } + } + return true; + } +}; +``` +### **Go** + +```go +func isConvex(points [][]int) bool { + n := len(points) + pre, cur := 0, 0 + for i := range points { + x1 := points[(i+1)%n][0] - points[i][0] + y1 := points[(i+1)%n][1] - points[i][1] + x2 := points[(i+2)%n][0] - points[i][0] + y2 := points[(i+2)%n][1] - points[i][1] + cur = x1*y2 - x2*y1 + if cur != 0 { + if cur*pre < 0 { + return false + } + pre = cur + } + } + return true +} ``` ### **...** diff --git a/solution/0400-0499/0469.Convex Polygon/Solution.cpp b/solution/0400-0499/0469.Convex Polygon/Solution.cpp new file mode 100644 index 0000000000000..fffa6bd1fe8cd --- /dev/null +++ b/solution/0400-0499/0469.Convex Polygon/Solution.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + bool isConvex(vector>& points) { + int n = points.size(); + long long pre = 0, cur = 0; + for (int i = 0; i < n; ++i) { + int x1 = points[(i + 1) % n][0] - points[i][0]; + int y1 = points[(i + 1) % n][1] - points[i][1]; + int x2 = points[(i + 2) % n][0] - points[i][0]; + int y2 = points[(i + 2) % n][1] - points[i][1]; + cur = 1L * x1 * y2 - x2 * y1; + if (cur != 0) { + if (cur * pre < 0) { + return false; + } + pre = cur; + } + } + return true; + } +}; \ No newline at end of file diff --git a/solution/0400-0499/0469.Convex Polygon/Solution.go b/solution/0400-0499/0469.Convex Polygon/Solution.go new file mode 100644 index 0000000000000..b9e4b9840e598 --- /dev/null +++ b/solution/0400-0499/0469.Convex Polygon/Solution.go @@ -0,0 +1,18 @@ +func isConvex(points [][]int) bool { + n := len(points) + pre, cur := 0, 0 + for i := range points { + x1 := points[(i+1)%n][0] - points[i][0] + y1 := points[(i+1)%n][1] - points[i][1] + x2 := points[(i+2)%n][0] - points[i][0] + y2 := points[(i+2)%n][1] - points[i][1] + cur = x1*y2 - x2*y1 + if cur != 0 { + if cur*pre < 0 { + return false + } + pre = cur + } + } + return true +} \ No newline at end of file diff --git a/solution/0400-0499/0469.Convex Polygon/Solution.java b/solution/0400-0499/0469.Convex Polygon/Solution.java new file mode 100644 index 0000000000000..988fbb325fe81 --- /dev/null +++ b/solution/0400-0499/0469.Convex Polygon/Solution.java @@ -0,0 +1,23 @@ +class Solution { + public boolean isConvex(List> points) { + int n = points.size(); + long pre = 0, cur = 0; + for (int i = 0; i < n; ++i) { + var p1 = points.get(i); + var p2 = points.get((i + 1) % n); + var p3 = points.get((i + 2) % n); + int x1 = p2.get(0) - p1.get(0); + int y1 = p2.get(1) - p1.get(1); + int x2 = p3.get(0) - p1.get(0); + int y2 = p3.get(1) - p1.get(1); + cur = x1 * y2 - x2 * y1; + if (cur != 0) { + if (cur * pre < 0) { + return false; + } + pre = cur; + } + } + return true; + } +} \ No newline at end of file diff --git a/solution/0400-0499/0469.Convex Polygon/Solution.py b/solution/0400-0499/0469.Convex Polygon/Solution.py new file mode 100644 index 0000000000000..addf509c5ad84 --- /dev/null +++ b/solution/0400-0499/0469.Convex Polygon/Solution.py @@ -0,0 +1,15 @@ +class Solution: + def isConvex(self, points: List[List[int]]) -> bool: + n = len(points) + pre = cur = 0 + for i in range(n): + x1 = points[(i + 1) % n][0] - points[i][0] + y1 = points[(i + 1) % n][1] - points[i][1] + x2 = points[(i + 2) % n][0] - points[i][0] + y2 = points[(i + 2) % n][1] - points[i][1] + cur = x1 * y2 - x2 * y1 + if cur != 0: + if cur * pre < 0: + return False + pre = cur + return True