-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckIfItIsAStraightLine_1232.java
34 lines (33 loc) · 1.14 KB
/
CheckIfItIsAStraightLine_1232.java
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
package org.example;
public class CheckIfItIsAStraightLine_1232 {
class Solution {
public boolean checkStraightLine(int[][] coordinates) {
int x = coordinates[0][0];
int y = coordinates[0][1];
for (int i=1; i<coordinates.length; i++){
if (coordinates[i][0] != x){
break;
}
if (i==coordinates.length-1){
return true;
}
}
for (int i=1; i<coordinates.length; i++){
if (coordinates[i][1] != y){
break;
}
if (i==coordinates.length-1){
return true;
}
}
double k = (double) (coordinates[1][1]-coordinates[0][1])/(coordinates[1][0]-coordinates[0][0]);
for (int i=1; i<coordinates.length-1; i++){
double temp = (double) (coordinates[i+1][1]-coordinates[i][1])/(coordinates[i+1][0]-coordinates[i][0]);
if (temp!=k){
return false;
}
}
return true;
}
}
}