|
40 | 40 |
|
41 | 41 | <!-- 这里可写通用的实现逻辑 -->
|
42 | 42 |
|
| 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 | + |
43 | 49 | <!-- tabs:start -->
|
44 | 50 |
|
45 | 51 | ### **Python3**
|
46 | 52 |
|
47 | 53 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
48 | 54 |
|
49 | 55 | ```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) |
51 | 60 | ```
|
52 | 61 |
|
53 | 62 | ### **Java**
|
54 | 63 |
|
55 | 64 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
56 | 65 |
|
57 | 66 | ```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** |
58 | 92 |
|
| 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 | +} |
59 | 100 | ```
|
60 | 101 |
|
61 | 102 | ### **...**
|
|
0 commit comments