-
Notifications
You must be signed in to change notification settings - Fork 8
/
leetcode1232_check_if_it_is_a_straight_line.cpp
70 lines (59 loc) · 1.84 KB
/
leetcode1232_check_if_it_is_a_straight_line.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
68
69
70
/**
* @file leetcode1232_check_if_it_is_a_straight_line.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2023/07/04 16:12
* @brief https://leetcode.com/problems/check-if-it-is-a-straight-line/
*
*/
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool checkStraightLine(vector<vector<int>>& coordinates) {
const auto& c = coordinates;
int length = c.size();
int b = 0;
int e = length - 1;
int x = c[e][0] - c[b][0];
int y = c[e][1] - c[b][1];
for (int i = 1; i < e; ++i) {
int dx = c[i][0] - c[b][0];
int dy = c[i][1] - c[b][1];
// If point i stands in the line formed by points b and e, then
// this equation must be true:
// y dy
// - = -
// x dx
//
// To deal with divide-by-zero problem, transform the equation a
// little
if (y * dx != x * dy) {
return false;
}
}
return true;
}
};
int main() {
while (1) {
int n = 0;
std::cout << "Input number of coordinates (Ctrl-C to exit): " << std::endl;
if (!(std::cin >> n)) {
return 0;
}
std::vector<std::vector<int>> coordinates;
for (int i = 0; i < n; ++i) {
std::cout << "x and y for coordinate " << i << std::endl;
std::vector<int> row;
int x;
int y;
std::cin >> x >> y;
coordinates.emplace_back(std::vector<int>({x, y}));
}
Solution solution;
bool ret = solution.checkStraightLine(coordinates);
std::cout << (ret == true ? "true": "false") << std::endl;
}
return 0;
}