-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay8.cpp
More file actions
37 lines (23 loc) · 733 Bytes
/
Copy pathDay8.cpp
File metadata and controls
37 lines (23 loc) · 733 Bytes
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
class Solution {
public:
bool checkStraightLine(vector<vector<int>>& coordinates) {
// formula of slope = (Y2 - Y1) / (X2 - X1)
// if the slope of the coordinates are same then they are in the straight line
if (coordinates.size() == 2) return true;
set <double> st;
for (int i = 0; i < coordinates.size()-1; i++) {
vector <int> tmp1 = coordinates[i];
vector <int> tmp2 = coordinates[i+1];
double x1 = tmp1[0], y1 = tmp1[1];
double x2 = tmp2[0], y2 = tmp2[1];
double slope = (y2 - y1) / (x2 - x1);
st.insert(slope);
}
if (st.size() == 1)
return true;
else
{
return false;
}
}
};