Skip to content

Commit 3d29602

Browse files
committed
feat: add solutions to lc problem: No.1232
No.1232.Check If It Is a Straight Line
1 parent c220a93 commit 3d29602

File tree

6 files changed

+160
-7
lines changed

6 files changed

+160
-7
lines changed

solution/1200-1299/1232.Check If It Is a Straight Line/README.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,80 @@
4343

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

46+
**方法一:数学**
47+
48+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 表示 `coordinates` 数组的长度。
49+
4650
<!-- tabs:start -->
4751

4852
### **Python3**
4953

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

5256
```python
53-
57+
class Solution:
58+
def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
59+
x1, y1 = coordinates[0]
60+
x2, y2 = coordinates[1]
61+
for x, y in coordinates[2:]:
62+
if (x - x1) * (y2 - y1) != (y - y1) * (x2 - x1):
63+
return False
64+
return True
5465
```
5566

5667
### **Java**
5768

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

6071
```java
72+
class Solution {
73+
public boolean checkStraightLine(int[][] coordinates) {
74+
int x1 = coordinates[0][0], y1 = coordinates[0][1];
75+
int x2 = coordinates[1][0], y2 = coordinates[1][1];
76+
for (int i = 2; i < coordinates.length; ++i) {
77+
int x = coordinates[i][0], y = coordinates[i][1];
78+
if ((x - x1) * (y2 - y1) != (y - y1) * (x2 - x1)) {
79+
return false;
80+
}
81+
}
82+
return true;
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
bool checkStraightLine(vector<vector<int>>& coordinates) {
93+
int x1 = coordinates[0][0], y1 = coordinates[0][1];
94+
int x2 = coordinates[1][0], y2 = coordinates[1][1];
95+
for (int i = 2; i < coordinates.size(); ++i) {
96+
int x = coordinates[i][0], y = coordinates[i][1];
97+
if ((x - x1) * (y2 - y1) != (y - y1) * (x2 - x1)) {
98+
return false;
99+
}
100+
}
101+
return true;
102+
}
103+
};
104+
```
61105
106+
### **Go**
107+
108+
```go
109+
func checkStraightLine(coordinates [][]int) bool {
110+
x1, y1 := coordinates[0][0], coordinates[0][1]
111+
x2, y2 := coordinates[1][0], coordinates[1][1]
112+
for i := 2; i < len(coordinates); i++ {
113+
x, y := coordinates[i][0], coordinates[i][1]
114+
if (x-x1)*(y2-y1) != (y-y1)*(x2-x1) {
115+
return false
116+
}
117+
}
118+
return true
119+
}
62120
```
63121

64122
### **...**

solution/1200-1299/1232.Check If It Is a Straight Line/README_EN.md

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,10 @@
3939
<p><strong>Constraints:</strong></p>
4040

4141
<ul>
42-
4342
<li><code>2 &lt;=&nbsp;coordinates.length &lt;= 1000</code></li>
44-
4543
<li><code>coordinates[i].length == 2</code></li>
46-
4744
<li><code>-10^4 &lt;=&nbsp;coordinates[i][0],&nbsp;coordinates[i][1] &lt;= 10^4</code></li>
48-
4945
<li><code>coordinates</code>&nbsp;contains no duplicate point.</li>
50-
5146
</ul>
5247

5348
## Solutions
@@ -57,13 +52,67 @@
5752
### **Python3**
5853

5954
```python
60-
55+
class Solution:
56+
def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
57+
x1, y1 = coordinates[0]
58+
x2, y2 = coordinates[1]
59+
for x, y in coordinates[2:]:
60+
if (x - x1) * (y2 - y1) != (y - y1) * (x2 - x1):
61+
return False
62+
return True
6163
```
6264

6365
### **Java**
6466

6567
```java
68+
class Solution {
69+
public boolean checkStraightLine(int[][] coordinates) {
70+
int x1 = coordinates[0][0], y1 = coordinates[0][1];
71+
int x2 = coordinates[1][0], y2 = coordinates[1][1];
72+
for (int i = 2; i < coordinates.length; ++i) {
73+
int x = coordinates[i][0], y = coordinates[i][1];
74+
if ((x - x1) * (y2 - y1) != (y - y1) * (x2 - x1)) {
75+
return false;
76+
}
77+
}
78+
return true;
79+
}
80+
}
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
bool checkStraightLine(vector<vector<int>>& coordinates) {
89+
int x1 = coordinates[0][0], y1 = coordinates[0][1];
90+
int x2 = coordinates[1][0], y2 = coordinates[1][1];
91+
for (int i = 2; i < coordinates.size(); ++i) {
92+
int x = coordinates[i][0], y = coordinates[i][1];
93+
if ((x - x1) * (y2 - y1) != (y - y1) * (x2 - x1)) {
94+
return false;
95+
}
96+
}
97+
return true;
98+
}
99+
};
100+
```
66101
102+
### **Go**
103+
104+
```go
105+
func checkStraightLine(coordinates [][]int) bool {
106+
x1, y1 := coordinates[0][0], coordinates[0][1]
107+
x2, y2 := coordinates[1][0], coordinates[1][1]
108+
for i := 2; i < len(coordinates); i++ {
109+
x, y := coordinates[i][0], coordinates[i][1]
110+
if (x-x1)*(y2-y1) != (y-y1)*(x2-x1) {
111+
return false
112+
}
113+
}
114+
return true
115+
}
67116
```
68117

69118
### **...**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
bool checkStraightLine(vector<vector<int>>& coordinates) {
4+
int x1 = coordinates[0][0], y1 = coordinates[0][1];
5+
int x2 = coordinates[1][0], y2 = coordinates[1][1];
6+
for (int i = 2; i < coordinates.size(); ++i) {
7+
int x = coordinates[i][0], y = coordinates[i][1];
8+
if ((x - x1) * (y2 - y1) != (y - y1) * (x2 - x1)) {
9+
return false;
10+
}
11+
}
12+
return true;
13+
}
14+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func checkStraightLine(coordinates [][]int) bool {
2+
x1, y1 := coordinates[0][0], coordinates[0][1]
3+
x2, y2 := coordinates[1][0], coordinates[1][1]
4+
for i := 2; i < len(coordinates); i++ {
5+
x, y := coordinates[i][0], coordinates[i][1]
6+
if (x-x1)*(y2-y1) != (y-y1)*(x2-x1) {
7+
return false
8+
}
9+
}
10+
return true
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public boolean checkStraightLine(int[][] coordinates) {
3+
int x1 = coordinates[0][0], y1 = coordinates[0][1];
4+
int x2 = coordinates[1][0], y2 = coordinates[1][1];
5+
for (int i = 2; i < coordinates.length; ++i) {
6+
int x = coordinates[i][0], y = coordinates[i][1];
7+
if ((x - x1) * (y2 - y1) != (y - y1) * (x2 - x1)) {
8+
return false;
9+
}
10+
}
11+
return true;
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
3+
x1, y1 = coordinates[0]
4+
x2, y2 = coordinates[1]
5+
for x, y in coordinates[2:]:
6+
if (x - x1) * (y2 - y1) != (y - y1) * (x2 - x1):
7+
return False
8+
return True

0 commit comments

Comments
 (0)