Check If It Is a Straight Line LeetCode solution in Java

1
class Solution {
2
public boolean checkStraightLine(int[][] coordinates) {
3
int x1 = coordinates[0][0];
4
int y1 = coordinates[0][1];
5

6
int x2 = coordinates[1][0];
7
int y2 = coordinates[1][1];
8

9
float slope;
10
if (x2 - x1 == 0) {
11
slope = Integer.MAX_VALUE;
12
} else {
13
slope = (y2 - y1) / (float) (x2 - x1);
14
}
15
for (int i = 0; i < coordinates.length; i++) {
16
for (int j = 0; j < 2; j++) {
17
if (slope == Integer.MAX_VALUE) {
18
if (coordinates[i][0] != x1) return false;
19
} else {
20
int y = coordinates[i][1];
21
int x = coordinates[i][0];
22
if ((float) (y - y1) != slope * (x - x1)) return false;
23
}
24
}
25
}
26
return true;
27
}
28
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0