Skip to content

Commit a108882

Browse files
add 766
1 parent 4d14d55 commit a108882

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|766|[Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_766.java) | O(m*n) | O(1) | |Easy|
2526
|765|[Couples Holding Hands](https://leetcode.com/problems/couples-holding-hands/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_765.java) | O(n^2) | O(1) | |Hard|
2627
|764|[Largest Plus Sign](https://leetcode.com/problems/largest-plus-sign/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_764.java) | O(n^2) | O(n^2) | |Medium| DP
2728
|763|[Partition Labels](https://leetcode.com/problems/partition-labels/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_763.java) | O(n) | O(1) | |Medium|
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.fishercoder.solutions;
2+
3+
/**766. Toeplitz Matrix
4+
*
5+
* A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element.
6+
7+
Now given an M x N matrix, return True if and only if the matrix is Toeplitz.
8+
9+
Example 1:
10+
11+
Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
12+
Output: True
13+
Explanation:
14+
1234
15+
5123
16+
9512
17+
18+
In the above grid, the diagonals are "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]",
19+
and in each diagonal all elements are the same, so the answer is True.
20+
21+
Example 2:
22+
23+
Input: matrix = [[1,2],[2,2]]
24+
Output: False
25+
Explanation:
26+
The diagonal "[1, 2]" has different elements.
27+
28+
Note:
29+
30+
matrix will be a 2D array of integers.
31+
matrix will have a number of rows and columns in range [1, 20].
32+
matrix[i][j] will be integers in range [0, 99].
33+
34+
*/
35+
public class _766 {
36+
public static class Solution1 {
37+
public boolean isToeplitzMatrix(int[][] matrix) {
38+
int m = matrix.length;
39+
int n = matrix[0].length;
40+
int i = 0;
41+
int j = 0;
42+
int sameVal = matrix[i][j];
43+
while (++i < m && ++j < n) {
44+
if (matrix[i][j] != sameVal) {
45+
return false;
46+
}
47+
}
48+
49+
for (i = 1, j = 0; i < m; i++) {
50+
int tmpI = i;
51+
int tmpJ = j;
52+
sameVal = matrix[i][j];
53+
while (++tmpI < m && ++tmpJ < n) {
54+
if (matrix[tmpI][tmpJ] != sameVal) {
55+
return false;
56+
}
57+
}
58+
}
59+
for (i = 0, j = 1; j < n; j++) {
60+
int tmpJ = j;
61+
int tmpI = i;
62+
sameVal = matrix[tmpI][tmpJ];
63+
while (++tmpI < m && ++tmpJ < n) {
64+
if (matrix[tmpI][tmpJ] != sameVal) {
65+
return false;
66+
}
67+
}
68+
}
69+
return true;
70+
}
71+
}
72+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._766;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _766Test {
10+
private static _766.Solution1 solution1;
11+
private static int[][] matrix;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _766.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
matrix = new int[][] {
21+
{1, 2, 3, 4},
22+
{5, 1, 2, 3},
23+
{9, 5, 1, 2}
24+
};
25+
assertEquals(true, solution1.isToeplitzMatrix(matrix));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
matrix = new int[][] {
31+
{1, 2},
32+
{2, 2},
33+
};
34+
assertEquals(false, solution1.isToeplitzMatrix(matrix));
35+
}
36+
37+
@Test
38+
public void test3() {
39+
matrix = new int[][] {
40+
{1, 2, 3, 4, 5, 9},
41+
{5, 1, 2, 3, 4, 5},
42+
{9, 5, 1, 2, 3, 4}
43+
};
44+
assertEquals(true, solution1.isToeplitzMatrix(matrix));
45+
}
46+
}

0 commit comments

Comments
 (0)