-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1232-CheckIfItIsAStraightLine.go
More file actions
84 lines (75 loc) · 2.41 KB
/
1232-CheckIfItIsAStraightLine.go
File metadata and controls
84 lines (75 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
// 1232. Check If It Is a Straight Line
// You are given an array coordinates, coordinates[i] = [x, y],
// where [x, y] represents the coordinate of a point.
// Check if these points make a straight line in the XY plane.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2019/10/15/untitled-diagram-2.jpg" />
// Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
// Output: true
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2019/10/09/untitled-diagram-1.jpg" />
// Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
// Output: false
// Constraints:
// 2 <= coordinates.length <= 1000
// coordinates[i].length == 2
// -10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
// coordinates contains no duplicate point.
import "fmt"
// func checkStraightLine(coordinates [][]int) bool {
// x, y := coordinates[1][0] - coordinates[0][0], coordinates[1][1] - coordinates[0][1]
// for i := 2 ; i < len(coordinates); i++ {
// // 差值不一样就不是在同一条直线上
// if coordinates[i][0] - coordinates[i-1][0] != x || coordinates[i][1] - coordinates[i-1][1] != y {
// return false
// }
// }
// return true
// }
func checkStraightLine(coordinates [][]int) bool {
// 平移 每个坐标都减去 coordinates[0]
// [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
// => [[1-1,2-2],[2-1,3-2], ..., [6-1,7-5]]
// => [[0 0] [1 1] [2 2] [3 3] [4 4] [5 5]]
x, y := coordinates[0][0], coordinates[0][1]
for _, v := range coordinates {
v[0] -= x
v[1] -= y
}
a, b := coordinates[1][1], -coordinates[1][0]
for _, v := range coordinates {
if a * v[0] + b * v[1] != 0 {
return false
}
}
return true
}
func main() {
// [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
fmt.Println(
checkStraightLine(
[][]int{
[]int{1,2},
[]int{2,3},
[]int{3,4},
[]int{4,5},
[]int{5,6},
[]int{6,7},
},
),
) // true
// [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
fmt.Println(
checkStraightLine(
[][]int{
[]int{1,1},
[]int{2,2},
[]int{3,4},
[]int{4,5},
[]int{5,6},
[]int{7,7},
},
),
) // false
}