Skip to content

Commit 3cc43f5

Browse files
authored
feat: add solutions to lc problem: No.0469 (doocs#1325)
No.0469.Convex Polygon
1 parent 850ea49 commit 3cc43f5

File tree

8 files changed

+259
-4
lines changed

8 files changed

+259
-4
lines changed

Diff for: solution/0400-0499/0418.Sentence Screen Fitting/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class Solution {
144144
public:
145145
int wordsTyping(vector<string>& sentence, int rows, int cols) {
146146
string s;
147-
for (auto& t: sentence) {
147+
for (auto& t : sentence) {
148148
s += t;
149149
s += " ";
150150
}

Diff for: solution/0400-0499/0418.Sentence Screen Fitting/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class Solution {
106106
public:
107107
int wordsTyping(vector<string>& sentence, int rows, int cols) {
108108
string s;
109-
for (auto& t: sentence) {
109+
for (auto& t : sentence) {
110110
s += t;
111111
s += " ";
112112
}

Diff for: solution/0400-0499/0469.Convex Polygon/README.md

+94-1
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,115 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
**方法一:数学(向量叉积)**
51+
52+
假设当前连续的三个顶点分别为 $p_1, p_2, p_3$,我们可以计算向量 $\overrightarrow{p_1p_2}$ 和 $\overrightarrow{p_1p_3}$ 的叉积,记为 $cur$。如果 $cur$ 的方向与之前的 $pre$ 方向不一致,说明多边形不是凸多边形。否则,我们更新 $pre = cur$,继续遍历下一个顶点。
53+
54+
遍历结束,如果没有发现不一致的情况,说明多边形是凸多边形。
55+
56+
时间复杂度 $O(n)$,其中 $n$ 是顶点的数量。空间复杂度 $O(1)$。
57+
5058
<!-- tabs:start -->
5159

5260
### **Python3**
5361

5462
<!-- 这里可写当前语言的特殊实现逻辑 -->
5563

5664
```python
57-
65+
class Solution:
66+
def isConvex(self, points: List[List[int]]) -> bool:
67+
n = len(points)
68+
pre = cur = 0
69+
for i in range(n):
70+
x1 = points[(i + 1) % n][0] - points[i][0]
71+
y1 = points[(i + 1) % n][1] - points[i][1]
72+
x2 = points[(i + 2) % n][0] - points[i][0]
73+
y2 = points[(i + 2) % n][1] - points[i][1]
74+
cur = x1 * y2 - x2 * y1
75+
if cur != 0:
76+
if cur * pre < 0:
77+
return False
78+
pre = cur
79+
return True
5880
```
5981

6082
### **Java**
6183

6284
<!-- 这里可写当前语言的特殊实现逻辑 -->
6385

6486
```java
87+
class Solution {
88+
public boolean isConvex(List<List<Integer>> points) {
89+
int n = points.size();
90+
long pre = 0, cur = 0;
91+
for (int i = 0; i < n; ++i) {
92+
var p1 = points.get(i);
93+
var p2 = points.get((i + 1) % n);
94+
var p3 = points.get((i + 2) % n);
95+
int x1 = p2.get(0) - p1.get(0);
96+
int y1 = p2.get(1) - p1.get(1);
97+
int x2 = p3.get(0) - p1.get(0);
98+
int y2 = p3.get(1) - p1.get(1);
99+
cur = x1 * y2 - x2 * y1;
100+
if (cur != 0) {
101+
if (cur * pre < 0) {
102+
return false;
103+
}
104+
pre = cur;
105+
}
106+
}
107+
return true;
108+
}
109+
}
110+
```
111+
112+
### **C++**
113+
114+
```cpp
115+
class Solution {
116+
public:
117+
bool isConvex(vector<vector<int>>& points) {
118+
int n = points.size();
119+
long long pre = 0, cur = 0;
120+
for (int i = 0; i < n; ++i) {
121+
int x1 = points[(i + 1) % n][0] - points[i][0];
122+
int y1 = points[(i + 1) % n][1] - points[i][1];
123+
int x2 = points[(i + 2) % n][0] - points[i][0];
124+
int y2 = points[(i + 2) % n][1] - points[i][1];
125+
cur = 1L * x1 * y2 - x2 * y1;
126+
if (cur != 0) {
127+
if (cur * pre < 0) {
128+
return false;
129+
}
130+
pre = cur;
131+
}
132+
}
133+
return true;
134+
}
135+
};
136+
```
65137
138+
### **Go**
139+
140+
```go
141+
func isConvex(points [][]int) bool {
142+
n := len(points)
143+
pre, cur := 0, 0
144+
for i := range points {
145+
x1 := points[(i+1)%n][0] - points[i][0]
146+
y1 := points[(i+1)%n][1] - points[i][1]
147+
x2 := points[(i+2)%n][0] - points[i][0]
148+
y2 := points[(i+2)%n][1] - points[i][1]
149+
cur = x1*y2 - x2*y1
150+
if cur != 0 {
151+
if cur*pre < 0 {
152+
return false
153+
}
154+
pre = cur
155+
}
156+
}
157+
return true
158+
}
66159
```
67160

68161
### **...**

Diff for: solution/0400-0499/0469.Convex Polygon/README_EN.md

+86-1
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,98 @@
4242
### **Python3**
4343

4444
```python
45-
45+
class Solution:
46+
def isConvex(self, points: List[List[int]]) -> bool:
47+
n = len(points)
48+
pre = cur = 0
49+
for i in range(n):
50+
x1 = points[(i + 1) % n][0] - points[i][0]
51+
y1 = points[(i + 1) % n][1] - points[i][1]
52+
x2 = points[(i + 2) % n][0] - points[i][0]
53+
y2 = points[(i + 2) % n][1] - points[i][1]
54+
cur = x1 * y2 - x2 * y1
55+
if cur != 0:
56+
if cur * pre < 0:
57+
return False
58+
pre = cur
59+
return True
4660
```
4761

4862
### **Java**
4963

5064
```java
65+
class Solution {
66+
public boolean isConvex(List<List<Integer>> points) {
67+
int n = points.size();
68+
long pre = 0, cur = 0;
69+
for (int i = 0; i < n; ++i) {
70+
var p1 = points.get(i);
71+
var p2 = points.get((i + 1) % n);
72+
var p3 = points.get((i + 2) % n);
73+
int x1 = p2.get(0) - p1.get(0);
74+
int y1 = p2.get(1) - p1.get(1);
75+
int x2 = p3.get(0) - p1.get(0);
76+
int y2 = p3.get(1) - p1.get(1);
77+
cur = x1 * y2 - x2 * y1;
78+
if (cur != 0) {
79+
if (cur * pre < 0) {
80+
return false;
81+
}
82+
pre = cur;
83+
}
84+
}
85+
return true;
86+
}
87+
}
88+
```
89+
90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
bool isConvex(vector<vector<int>>& points) {
96+
int n = points.size();
97+
long long pre = 0, cur = 0;
98+
for (int i = 0; i < n; ++i) {
99+
int x1 = points[(i + 1) % n][0] - points[i][0];
100+
int y1 = points[(i + 1) % n][1] - points[i][1];
101+
int x2 = points[(i + 2) % n][0] - points[i][0];
102+
int y2 = points[(i + 2) % n][1] - points[i][1];
103+
cur = 1L * x1 * y2 - x2 * y1;
104+
if (cur != 0) {
105+
if (cur * pre < 0) {
106+
return false;
107+
}
108+
pre = cur;
109+
}
110+
}
111+
return true;
112+
}
113+
};
114+
```
51115
116+
### **Go**
117+
118+
```go
119+
func isConvex(points [][]int) bool {
120+
n := len(points)
121+
pre, cur := 0, 0
122+
for i := range points {
123+
x1 := points[(i+1)%n][0] - points[i][0]
124+
y1 := points[(i+1)%n][1] - points[i][1]
125+
x2 := points[(i+2)%n][0] - points[i][0]
126+
y2 := points[(i+2)%n][1] - points[i][1]
127+
cur = x1*y2 - x2*y1
128+
if cur != 0 {
129+
if cur*pre < 0 {
130+
return false
131+
}
132+
pre = cur
133+
}
134+
}
135+
return true
136+
}
52137
```
53138

54139
### **...**

Diff for: solution/0400-0499/0469.Convex Polygon/Solution.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
bool isConvex(vector<vector<int>>& points) {
4+
int n = points.size();
5+
long long pre = 0, cur = 0;
6+
for (int i = 0; i < n; ++i) {
7+
int x1 = points[(i + 1) % n][0] - points[i][0];
8+
int y1 = points[(i + 1) % n][1] - points[i][1];
9+
int x2 = points[(i + 2) % n][0] - points[i][0];
10+
int y2 = points[(i + 2) % n][1] - points[i][1];
11+
cur = 1L * x1 * y2 - x2 * y1;
12+
if (cur != 0) {
13+
if (cur * pre < 0) {
14+
return false;
15+
}
16+
pre = cur;
17+
}
18+
}
19+
return true;
20+
}
21+
};

Diff for: solution/0400-0499/0469.Convex Polygon/Solution.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func isConvex(points [][]int) bool {
2+
n := len(points)
3+
pre, cur := 0, 0
4+
for i := range points {
5+
x1 := points[(i+1)%n][0] - points[i][0]
6+
y1 := points[(i+1)%n][1] - points[i][1]
7+
x2 := points[(i+2)%n][0] - points[i][0]
8+
y2 := points[(i+2)%n][1] - points[i][1]
9+
cur = x1*y2 - x2*y1
10+
if cur != 0 {
11+
if cur*pre < 0 {
12+
return false
13+
}
14+
pre = cur
15+
}
16+
}
17+
return true
18+
}

Diff for: solution/0400-0499/0469.Convex Polygon/Solution.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public boolean isConvex(List<List<Integer>> points) {
3+
int n = points.size();
4+
long pre = 0, cur = 0;
5+
for (int i = 0; i < n; ++i) {
6+
var p1 = points.get(i);
7+
var p2 = points.get((i + 1) % n);
8+
var p3 = points.get((i + 2) % n);
9+
int x1 = p2.get(0) - p1.get(0);
10+
int y1 = p2.get(1) - p1.get(1);
11+
int x2 = p3.get(0) - p1.get(0);
12+
int y2 = p3.get(1) - p1.get(1);
13+
cur = x1 * y2 - x2 * y1;
14+
if (cur != 0) {
15+
if (cur * pre < 0) {
16+
return false;
17+
}
18+
pre = cur;
19+
}
20+
}
21+
return true;
22+
}
23+
}

Diff for: solution/0400-0499/0469.Convex Polygon/Solution.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def isConvex(self, points: List[List[int]]) -> bool:
3+
n = len(points)
4+
pre = cur = 0
5+
for i in range(n):
6+
x1 = points[(i + 1) % n][0] - points[i][0]
7+
y1 = points[(i + 1) % n][1] - points[i][1]
8+
x2 = points[(i + 2) % n][0] - points[i][0]
9+
y2 = points[(i + 2) % n][1] - points[i][1]
10+
cur = x1 * y2 - x2 * y1
11+
if cur != 0:
12+
if cur * pre < 0:
13+
return False
14+
pre = cur
15+
return True

0 commit comments

Comments
 (0)