Skip to content

Commit e1987c4

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0766
1 parent 0bb9216 commit e1987c4

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

solution/0700-0799/0766.Toeplitz Matrix/README.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,43 @@ matrix = [
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
遍历矩阵,若出现元素与其左上角的元素不等的情况,返回 `false`
59+
5860
<!-- tabs:start -->
5961

6062
### **Python3**
6163

6264
<!-- 这里可写当前语言的特殊实现逻辑 -->
6365

6466
```python
65-
67+
class Solution:
68+
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
69+
m, n = len(matrix), len(matrix[0])
70+
for i in range(1, m):
71+
for j in range(1, n):
72+
if matrix[i][j] != matrix[i - 1][j - 1]:
73+
return False
74+
return True
6675
```
6776

6877
### **Java**
6978

7079
<!-- 这里可写当前语言的特殊实现逻辑 -->
7180

7281
```java
73-
82+
class Solution {
83+
public boolean isToeplitzMatrix(int[][] matrix) {
84+
int m = matrix.length, n = matrix[0].length;
85+
for (int i = 1; i < m; ++i) {
86+
for (int j = 1; j < n; ++j) {
87+
if (matrix[i][j] != matrix[i - 1][j - 1]) {
88+
return false;
89+
}
90+
}
91+
}
92+
return true;
93+
}
94+
}
7495
```
7596

7697
### **...**

solution/0700-0799/0766.Toeplitz Matrix/README_EN.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,32 @@ The diagonal &quot;[1, 2]&quot; has different elements.
8686
### **Python3**
8787

8888
```python
89-
89+
class Solution:
90+
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
91+
m, n = len(matrix), len(matrix[0])
92+
for i in range(1, m):
93+
for j in range(1, n):
94+
if matrix[i][j] != matrix[i - 1][j - 1]:
95+
return False
96+
return True
9097
```
9198

9299
### **Java**
93100

94101
```java
95-
102+
class Solution {
103+
public boolean isToeplitzMatrix(int[][] matrix) {
104+
int m = matrix.length, n = matrix[0].length;
105+
for (int i = 1; i < m; ++i) {
106+
for (int j = 1; j < n; ++j) {
107+
if (matrix[i][j] != matrix[i - 1][j - 1]) {
108+
return false;
109+
}
110+
}
111+
}
112+
return true;
113+
}
114+
}
96115
```
97116

98117
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public boolean isToeplitzMatrix(int[][] matrix) {
3+
int m = matrix.length, n = matrix[0].length;
4+
for (int i = 1; i < m; ++i) {
5+
for (int j = 1; j < n; ++j) {
6+
if (matrix[i][j] != matrix[i - 1][j - 1]) {
7+
return false;
8+
}
9+
}
10+
}
11+
return true;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
3+
m, n = len(matrix), len(matrix[0])
4+
for i in range(1, m):
5+
for j in range(1, n):
6+
if matrix[i][j] != matrix[i - 1][j - 1]:
7+
return False
8+
return True

0 commit comments

Comments
 (0)