Skip to content

Commit 2e69f3a

Browse files
add 1232
1 parent 5056af8 commit 2e69f3a

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ _If you like this project, please leave me a star._ ★
1313
|1252|[Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1252.java) | O(m*n + k) | O(m*n) | |Easy||
1414
|1237|[Find Positive Integer Solution for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1237.java) | | | |Easy||
1515
|1243|[Array Transformation](https://leetcode.com/problems/array-transformation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1243.java) | | | [:tv:](https://www.youtube.com/watch?v=MQ2i4T1l-Gs)|Easy||
16+
|1232|[Check If It Is a Straight Line](https://leetcode.com/problems/check-if-it-is-a-straight-line/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1232.java) | | | |Easy||
1617
|1217|[Play with Chips](https://leetcode.com/problems/play-with-chips/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1217.java) | | | |Easy||
1718
|1213|[Intersection of Three Sorted Arrays](https://leetcode.com/problems/intersection-of-three-sorted-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1213.java) | | | [:tv:](https://www.youtube.com/watch?v=zceoOrHSHNQ)|Easy||
1819
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1207.java) | O(n) | O(1) | [:tv:](https://www.youtube.com/watch?v=_NYimlZY1PE)|Easy||
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1232. Check If It Is a Straight Line
5+
*
6+
* You are given an array coordinates, coordinates[i] = [x, y],
7+
* where [x, y] represents the coordinate of a point.
8+
* Check if these points make a straight line in the XY plane.
9+
*
10+
* Example 1:
11+
* Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
12+
* Output: true
13+
*
14+
* Example 2:
15+
* Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
16+
* Output: false
17+
*
18+
* Constraints:
19+
* 2 <= coordinates.length <= 1000
20+
* coordinates[i].length == 2
21+
* -10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
22+
* coordinates contains no duplicate point.
23+
* */
24+
public class _1232 {
25+
public static class Solution1 {
26+
/**
27+
* To check if they share the same slope, we use this formula:
28+
*
29+
* check whether (y4 - y3)/(x4- x3) equals to (y2 - y1)/(x2 - x1)
30+
* considering denominator could be zero, we'll change it to use multiplication instead of division,
31+
* thus it becomes
32+
* check whether (y4 - y3)*(x2 - x1) equals (x4 - x3)*(y2 - y1)
33+
* */
34+
public boolean checkStraightLine(int[][] coordinates) {
35+
for (int i = 2; i < coordinates.length - 1; i++) {
36+
if ((coordinates[1][0] - coordinates[0][0]) * (coordinates[i + 1][1] - coordinates[i][1]) !=
37+
(coordinates[1][1] - coordinates[0][1]) * (coordinates[i + 1][0] - coordinates[i][0])) {
38+
return false;
39+
}
40+
}
41+
return true;
42+
}
43+
}
44+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1232;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1232Test {
10+
private static _1232.Solution1 solution1;
11+
private static int[][] coordinates;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1232.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
coordinates = new int[][]{
21+
{1, 2},
22+
{2, 3},
23+
{3, 4},
24+
{4, 5},
25+
{5, 6},
26+
{6, 7}
27+
};
28+
assertEquals(true, solution1.checkStraightLine(coordinates));
29+
}
30+
31+
@Test
32+
public void test2() {
33+
coordinates = new int[][]{
34+
{1, 1},
35+
{2, 2},
36+
{3, 4},
37+
{4, 5},
38+
{5, 6},
39+
{7, 7}
40+
};
41+
assertEquals(false, solution1.checkStraightLine(coordinates));
42+
}
43+
44+
@Test
45+
public void test3() {
46+
coordinates = new int[][]{
47+
{-3, -2},
48+
{-1, -2},
49+
{2, -2},
50+
{-2, -2},
51+
{0, -2}
52+
};
53+
assertEquals(true, solution1.checkStraightLine(coordinates));
54+
}
55+
56+
@Test
57+
public void test4() {
58+
coordinates = new int[][]{
59+
{0, 1},
60+
{1, 3},
61+
{-4, -7},
62+
{5, 11}
63+
};
64+
assertEquals(true, solution1.checkStraightLine(coordinates));
65+
}
66+
67+
}

0 commit comments

Comments
 (0)