Skip to content

feat: add solutions to lc problem: No.0469 #1325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion solution/0400-0499/0418.Sentence Screen Fitting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class Solution {
public:
int wordsTyping(vector<string>& sentence, int rows, int cols) {
string s;
for (auto& t: sentence) {
for (auto& t : sentence) {
s += t;
s += " ";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Solution {
public:
int wordsTyping(vector<string>& sentence, int rows, int cols) {
string s;
for (auto& t: sentence) {
for (auto& t : sentence) {
s += t;
s += " ";
}
Expand Down
95 changes: 94 additions & 1 deletion solution/0400-0499/0469.Convex Polygon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,115 @@

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

**方法一:数学(向量叉积)**

假设当前连续的三个顶点分别为 $p_1, p_2, p_3$,我们可以计算向量 $\overrightarrow{p_1p_2}$ 和 $\overrightarrow{p_1p_3}$ 的叉积,记为 $cur$。如果 $cur$ 的方向与之前的 $pre$ 方向不一致,说明多边形不是凸多边形。否则,我们更新 $pre = cur$,继续遍历下一个顶点。

遍历结束,如果没有发现不一致的情况,说明多边形是凸多边形。

时间复杂度 $O(n)$,其中 $n$ 是顶点的数量。空间复杂度 $O(1)$。

<!-- tabs:start -->

### **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<List<Integer>> 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<vector<int>>& 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
}
```

### **...**
Expand Down
87 changes: 86 additions & 1 deletion solution/0400-0499/0469.Convex Polygon/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Integer>> 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<vector<int>>& 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
}
```

### **...**
Expand Down
21 changes: 21 additions & 0 deletions solution/0400-0499/0469.Convex Polygon/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
bool isConvex(vector<vector<int>>& 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;
}
};
18 changes: 18 additions & 0 deletions solution/0400-0499/0469.Convex Polygon/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 23 additions & 0 deletions solution/0400-0499/0469.Convex Polygon/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public boolean isConvex(List<List<Integer>> 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;
}
}
15 changes: 15 additions & 0 deletions solution/0400-0499/0469.Convex Polygon/Solution.py
Original file line number Diff line number Diff line change
@@ -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