Skip to content

Commit d56a105

Browse files
committed
feat: add the solution of Toeplitz Matrix(766) with Java.
1 parent 542ca1b commit d56a105

File tree

3 files changed

+68
-24
lines changed

3 files changed

+68
-24
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
| [746][746-question] | [Min Cost Climbing Stairs][746-tips] | [][746-java] | [][746-js] | |
8989
| [747][747-question] | [Largest Number At Least Twice of Others][747-tips] | [][747-java] | [][747-js] | |
9090
| [762][762-question] | [Prime Number of Set Bits in Binary Representation][762-tips]| [][762-java] | [][762-js] | |
91-
| [766][766-question] | [Toeplitz Matrix][766-tips] | | [][766-js] | |
91+
| [766][766-question] | [Toeplitz Matrix][766-tips] | [][766-java] | [][766-js] | |
9292
| [771][771-question] | [Jewels and Stones][771-tips] | [][771-java] | [][771-js] | [][771-kotlin] |
9393
| [783][783-question] | [Minimum Distance Between BST Nodes][783-tips] | | [][783-js] | |
9494
| [784][784-question] | [Letter Case Permutation][784-tips] | | [][784-js] | |
@@ -486,6 +486,7 @@
486486
[746-java]: ./src/_746/Solution.java
487487
[747-java]: ./src/_747/Solution.java
488488
[762-java]: ./src/_762/Solution.java
489+
[762-java]: ./src/_766/Solution.java
489490
[771-java]: ./src/_771/Solution.java
490491
[804-java]: ./src/_804/Solution.java
491492

src/_766/Solution.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package _766;
2+
3+
class Solution {
4+
public boolean isToeplitzMatrix(int[][] matrix) {
5+
int h = matrix.length - 1;
6+
int w = matrix[0].length - 1;
7+
for (int i = 0; i < h; i++) {
8+
for (int j = 0; j < w; j++) {
9+
if (matrix[i][j] != matrix[i + 1][j + 1]) {
10+
return false;
11+
}
12+
}
13+
}
14+
return true;
15+
}
16+
17+
public static void main(String[] args) {
18+
Solution solution = new Solution();
19+
System.out.println(solution.isToeplitzMatrix(new int[][]{{1, 2}, {2, 2}}));
20+
System.out.println(solution.isToeplitzMatrix(new int[][]{{1, 2, 3, 4}, {5, 1, 2, 3}, {9, 5, 1, 2}}));
21+
}
22+
}

tips/766/README.md

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,67 @@
1-
[xxxx][title]
1+
[Toeplitz Matrix][title]
22

33
## Description
4-
// 抄题目
54

5+
A matrix is *Toeplitz* if every diagonal from top-left to bottom-right has the same element.
66

7-
**Example:**
7+
Now given an `M x N` matrix, return `True` if and only if the matrix is *Toeplitz*.
8+
9+
**Example 1:**
810

911
```
10-
// 抄Example
12+
Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
13+
Output: True
14+
Explanation:
15+
1234
16+
5123
17+
9512
18+
19+
In the above grid, the diagonals are "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]", and in each diagonal all elements are the same, so the answer is True.
1120
```
1221

13-
**Note:**
14-
// Note
22+
**Example 2:**
1523

16-
**Tags:** // tags
24+
```
25+
Input: matrix = [[1,2],[2,2]]
26+
Output: False
27+
Explanation:
28+
The diagonal "[1, 2]" has different elements.
29+
```
1730

31+
**Note:**
1832

19-
## 思路 1
20-
// 贴一些关键代码,说一些解题思路
21-
// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22-
```java
33+
1. `matrix` will be a 2D array of integers.
34+
2. `matrix` will have a number of rows and columns in range `[1, 20]`.
35+
3. `matrix[i][j]` will be integers in range `[0, 99]`.
2336

24-
```
25-
```javascript
37+
**Tags:** [Array](https://leetcode.com/tag/array/)
2638

27-
```
39+
## 思路
2840

29-
## 思路 2
30-
// 贴一些关键代码,说一些解题思路
31-
```java
41+
给出一个二维数组,判断每一条左上至右下的对角线上的数是否是一致的。
3242

33-
```
43+
遍历数组,判断每个数是否与其右下角的数一致即可。注意边界判断。
3444

35-
## 思路 3
36-
// 贴一些关键代码,说一些解题思路
37-
```kotlin
45+
**Java:**
3846

47+
```java
48+
public boolean isToeplitzMatrix(int[][] matrix) {
49+
int h = matrix.length - 1;
50+
int w = matrix[0].length - 1;
51+
for (int i = 0; i < h; i++) {
52+
for (int j = 0; j < w; j++) {
53+
if (matrix[i][j] != matrix[i + 1][j + 1]) {
54+
return false;
55+
}
56+
}
57+
}
58+
return true;
59+
}
3960
```
4061

4162
## 结语
42-
63+
4364
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
4465

45-
[title]: https://leetcode.com/problems/xxxx
66+
[title]: https://leetcode.com/problems/toeplitz-matrix/description/
4667
[ls]: https://github.com/RichCodersAndMe/LeetCode-Solution

0 commit comments

Comments
 (0)