-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 1232-Check_If_It_Is_A_Straight_Line.java array
- Loading branch information
1 parent
b8af8e3
commit 00fb6a7
Showing
1 changed file
with
20 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |