Skip to content

Commit

Permalink
Add 1232-Check_If_It_Is_A_Straight_Line.java array
Browse files Browse the repository at this point in the history
  • Loading branch information
trend-carolkao committed May 26, 2021
1 parent b8af8e3 commit 00fb6a7
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions 1232-Check_If_It_Is_A_Straight_Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Tag: Array
class Solution {
public boolean checkStraightLine(int[][] coordinates) {
if (coordinates.length <= 2) return true;

int x0 = coordinates[0][0], y0 = coordinates[0][1];
int diffX = coordinates[1][0] - x0;
int diffY = coordinates[1][1] - y0;
for (int i = 2; i < coordinates.length; i++) {
int tmpDiffX = coordinates[i][0] - x0;
int tmpDiffY = coordinates[i][1] - y0;

// x1 : y1 == x2 : y2
// ==> x1 * y2 == y1 * x2
if (diffX * tmpDiffY != diffY * tmpDiffX) return false;
}

return true;
}
}

0 comments on commit 00fb6a7

Please sign in to comment.