Skip to content

Commit 056bc56

Browse files
solves check if is on straight line
1 parent c9219a5 commit 056bc56

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@
321321
| 1213 | 🔒 [Intersection of Three Sorted Arrays](https://leetcode.com/problems/intersection-of-three-sorted-arrays) | | |
322322
| 1217 | [Minimum Cost to Move Chips to The Same Position](https://leetcode.com/problems/minimum-cost-to-move-chips-to-the-same-position) | [![Java](assets/java.png)](src/MinimumCostToMoveChipsToTheSamePosition.java) | |
323323
| 1221 | [Split A String In Balanced Strings](https://leetcode.com/problems/split-a-string-in-balanced-strings) | [![Java](assets/java.png)](src/SplitAStringInBalancedStrings.java) | |
324-
| 1228 | [Missing A Number In Arithmetic Progression](https://leetcode.com/problems/missing-number-in-arithmetic-progression) | | |
325-
| 1232 | [Check If It Is A Straight Line](https://leetcode.com/problems/check-if-it-is-a-straight-line) | | |
324+
| 1228 | 🔒 [Missing A Number In Arithmetic Progression](https://leetcode.com/problems/missing-number-in-arithmetic-progression) | | |
325+
| 1232 | [Check If It Is A Straight Line](https://leetcode.com/problems/check-if-it-is-a-straight-line) | [![Java](assets/java.png)](src/CheckIfItIsASStraightLine.java) | |
326326
| 1237 | [Find Positive Integer Solutions for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation) | | |
327327
| 1243 | [Array Transformation](https://leetcode.com/problems/array-transformation) | | |
328328
| 1252 | [Cells With Odd Values In Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix) | | |

src/CheckIfItIsASStraightLine.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class CheckIfItIsASStraightLine {
2+
public boolean checkStraightLine(int[][] coordinates) {
3+
for (int i = 0 ; i < coordinates.length - 2 ; i++) {
4+
if (!isOnStraightLine(coordinates[i], coordinates[i + 1], coordinates[i + 2])) {
5+
return false;
6+
}
7+
}
8+
return true;
9+
}
10+
11+
private boolean isOnStraightLine(int[] p1, int[] p2, int[] p3) {
12+
if (p1[0] == p2[0] && p2[0] == p3[0]) return true;
13+
if (p1[1] == p2[1] && p2[1] == p3[1]) return true;
14+
return (p2[1] - p1[1]) * (p3[0] - p2[0]) == (p3[1] - p2[1]) * (p2[0] - p1[0]);
15+
}
16+
}

0 commit comments

Comments
 (0)