Skip to content

Commit ef2d89d

Browse files
committed
feat: add solutions to lc problem: No.1037
No.1037.Valid Boomerang
1 parent 21f010f commit ef2d89d

File tree

6 files changed

+102
-43
lines changed

6 files changed

+102
-43
lines changed

solution/1000-1099/1037.Valid Boomerang/README.md

+42-1
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,63 @@
4040

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

43+
**方法一:斜率比较**
44+
45+
设三点分别为 $(x_1,y_1)$, $(x_2,y_2)$, $(x_3,y_3)$。
46+
47+
要使得三点不共线,需要满足 $\frac{y_2-y_1}{x_2-x_1}\neq\frac{y_3-y_2}{x_3-x_2}$,变形得 $(y_2-y_1)*(x_3-x_2) \neq (y_3-y_2)*(x_2-x_1)$。
48+
4349
<!-- tabs:start -->
4450

4551
### **Python3**
4652

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

4955
```python
50-
56+
class Solution:
57+
def isBoomerang(self, points: List[List[int]]) -> bool:
58+
(x1, y1), (x2, y2), (x3, y3) = points
59+
return (y2 - y1) * (x3 - x2) != (y3 - y2) * (x2 - x1)
5160
```
5261

5362
### **Java**
5463

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

5766
```java
67+
class Solution {
68+
public boolean isBoomerang(int[][] points) {
69+
int x1 = points[0][0], y1 = points[0][1];
70+
int x2 = points[1][0], y2 = points[1][1];
71+
int x3 = points[2][0], y3 = points[2][1];
72+
return (y2 - y1) * (x3 - x2) != (y3 - y2) * (x2 - x1);
73+
}
74+
}
75+
```
76+
77+
### **C++**
78+
79+
```cpp
80+
class Solution {
81+
public:
82+
bool isBoomerang(vector<vector<int>>& points) {
83+
int x1 = points[0][0], y1 = points[0][1];
84+
int x2 = points[1][0], y2 = points[1][1];
85+
int x3 = points[2][0], y3 = points[2][1];
86+
return (y2 - y1) * (x3 - x2) != (y3 - y2) * (x2 - x1);
87+
}
88+
};
89+
```
90+
91+
### **Go**
5892
93+
```go
94+
func isBoomerang(points [][]int) bool {
95+
x1, y1 := points[0][0], points[0][1]
96+
x2, y2 := points[1][0], points[1][1]
97+
x3, y3 := points[2][0], points[2][1]
98+
return (y2-y1)*(x3-x2) != (y3-y2)*(x2-x1)
99+
}
59100
```
60101

61102
### **...**

solution/1000-1099/1037.Valid Boomerang/README_EN.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,48 @@
3232
### **Python3**
3333

3434
```python
35-
35+
class Solution:
36+
def isBoomerang(self, points: List[List[int]]) -> bool:
37+
(x1, y1), (x2, y2), (x3, y3) = points
38+
return (y2 - y1) * (x3 - x2) != (y3 - y2) * (x2 - x1)
3639
```
3740

3841
### **Java**
3942

4043
```java
44+
class Solution {
45+
public boolean isBoomerang(int[][] points) {
46+
int x1 = points[0][0], y1 = points[0][1];
47+
int x2 = points[1][0], y2 = points[1][1];
48+
int x3 = points[2][0], y3 = points[2][1];
49+
return (y2 - y1) * (x3 - x2) != (y3 - y2) * (x2 - x1);
50+
}
51+
}
52+
```
53+
54+
### **C++**
55+
56+
```cpp
57+
class Solution {
58+
public:
59+
bool isBoomerang(vector<vector<int>>& points) {
60+
int x1 = points[0][0], y1 = points[0][1];
61+
int x2 = points[1][0], y2 = points[1][1];
62+
int x3 = points[2][0], y3 = points[2][1];
63+
return (y2 - y1) * (x3 - x2) != (y3 - y2) * (x2 - x1);
64+
}
65+
};
66+
```
67+
68+
### **Go**
4169
70+
```go
71+
func isBoomerang(points [][]int) bool {
72+
x1, y1 := points[0][0], points[0][1]
73+
x2, y2 := points[1][0], points[1][1]
74+
x3, y3 := points[2][0], points[2][1]
75+
return (y2-y1)*(x3-x2) != (y3-y2)*(x2-x1)
76+
}
4277
```
4378

4479
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
bool isBoomerang(vector<vector<int>>& points) {
4+
int x1 = points[0][0], y1 = points[0][1];
5+
int x2 = points[1][0], y2 = points[1][1];
6+
int x3 = points[2][0], y3 = points[2][1];
7+
return (y2 - y1) * (x3 - x2) != (y3 - y2) * (x2 - x1);
8+
}
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
func isBoomerang(points [][]int) bool {
2+
x1, y1 := points[0][0], points[0][1]
3+
x2, y2 := points[1][0], points[1][1]
4+
x3, y3 := points[2][0], points[2][1]
5+
return (y2-y1)*(x3-x2) != (y3-y2)*(x2-x1)
6+
}
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,8 @@
11
class Solution {
22
public boolean isBoomerang(int[][] points) {
3-
double temp1;
4-
double temp2;
5-
double temp3;
6-
7-
Arrays.sort(points, new Comparator<int[]>() {
8-
@Override
9-
public int compare(int[] ints, int[] t1) {
10-
return ints[0] - t1[0];
11-
}
12-
});
13-
14-
if (points[0][0] == points[1][0]) {
15-
if (points[0][1] == points[1][1])
16-
return false;
17-
temp1 = 1;
18-
} else {
19-
temp1 = (points[0][1] - points[1][1]) * 1.0 / (points[0][0] - points[1][0]);
20-
}
21-
22-
if (points[1][0] == points[2][0]) {
23-
if (points[1][1] == points[2][1])
24-
return false;
25-
temp2 = 1;
26-
} else {
27-
temp2 = (points[1][1] - points[2][1]) * 1.0 / (points[1][0] - points[2][0]);
28-
}
29-
30-
if (points[0][0] == points[2][0]) {
31-
if (points[0][1] == points[2][1])
32-
return false;
33-
temp3 = 1;
34-
} else {
35-
temp3 = (points[0][1] - points[2][1]) * 1.0 / (points[0][0] - points[2][0]);
36-
}
37-
38-
if (temp1 == temp2 && temp1 == temp3 && temp2 == temp3) {
39-
return false;
40-
} else {
41-
return true;
42-
}
3+
int x1 = points[0][0], y1 = points[0][1];
4+
int x2 = points[1][0], y2 = points[1][1];
5+
int x3 = points[2][0], y3 = points[2][1];
6+
return (y2 - y1) * (x3 - x2) != (y3 - y2) * (x2 - x1);
437
}
44-
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def isBoomerang(self, points: List[List[int]]) -> bool:
3+
(x1, y1), (x2, y2), (x3, y3) = points
4+
return (y2 - y1) * (x3 - x2) != (y3 - y2) * (x2 - x1)

0 commit comments

Comments
 (0)