-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay8. CheckIfItIsAStraightLine.cpp
67 lines (53 loc) · 1.87 KB
/
Day8. CheckIfItIsAStraightLine.cpp
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
/*
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:
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:
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:
1. 2 <= coordinates.length <= 1000
2. coordinates[i].length == 2
3. -10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
4. coordinates contains no duplicate point.
Hide Hint #1
If there're only 2 points, return true.
Hide Hint #2
Check if all other points lie on the line defined by the first 2 points.
Hide Hint #3
Use cross product to check collinearity.
*/
class Solution {
public:
bool checkStraightLine(vector<vector<int>>& coordinates) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(coordinates.size()<=2) return true;
double deno=(coordinates[1][1]-coordinates[0][1]);
double numo=(coordinates[1][0]-coordinates[0][0]);
int x1=coordinates[0][0];
int y1=coordinates[0][1];
if(deno==0){
for(int i=2;i<coordinates.size();i++){
if(coordinates[i][1]!=coordinates[0][1]) return false;
}
}else if(numo==0){
for(int i=2;i<coordinates.size();i++){
if(coordinates[i][0]!=coordinates[0][0]) return false;
}
}else{
double m=numo/deno;
double curr;
for(int i=2;i<coordinates.size();i++){
curr=(double)(coordinates[i][0]-x1)/(double)(coordinates[i][1]-y1);
if(curr!=m) return false;
}
}
return true;
}
};