Skip to content

Commit 86f6f28

Browse files
committed
feat: add solutions to lc problem: No.0766
No.0766.Toeplitz Matrix
1 parent 4eba269 commit 86f6f28

File tree

6 files changed

+122
-29
lines changed

6 files changed

+122
-29
lines changed

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

+45-12
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ tags:
2929
<strong>输入:</strong>matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
3030
<strong>输出:</strong>true
3131
<strong>解释:</strong>
32-
在上述矩阵中, 其对角线为:
33-
"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]"。
32+
在上述矩阵中, 其对角线为:
33+
"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]"。
3434
各条对角线上的所有元素均相同, 因此答案是 True 。
3535
</pre>
3636

@@ -70,9 +70,9 @@ tags:
7070

7171
### 方法一:一次遍历
7272

73-
遍历矩阵,若出现元素与其左上角的元素不等的情况,返回 `false`。否则,遍历结束后返回 `true`
73+
根据题目描述,托普利茨矩阵的特点是:矩阵中每个元素都与其左上角的元素相等。因此,我们只需要遍历矩阵中的每个元素,检查它是否与左上角的元素相等即可
7474

75-
时间复杂度 $O(m \times n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数
75+
时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$
7676

7777
<!-- tabs:start -->
7878

@@ -82,11 +82,11 @@ tags:
8282
class Solution:
8383
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
8484
m, n = len(matrix), len(matrix[0])
85-
return all(
86-
matrix[i][j] == matrix[i - 1][j - 1]
87-
for i in range(1, m)
88-
for j in range(1, n)
89-
)
85+
for i in range(1, m):
86+
for j in range(1, n):
87+
if matrix[i][j] != matrix[i - 1][j - 1]:
88+
return False
89+
return True
9090
```
9191

9292
#### Java
@@ -142,6 +142,40 @@ func isToeplitzMatrix(matrix [][]int) bool {
142142
}
143143
```
144144

145+
#### TypeScript
146+
147+
```ts
148+
function isToeplitzMatrix(matrix: number[][]): boolean {
149+
const [m, n] = [matrix.length, matrix[0].length];
150+
for (let i = 1; i < m; ++i) {
151+
for (let j = 1; j < n; ++j) {
152+
if (matrix[i][j] !== matrix[i - 1][j - 1]) {
153+
return false;
154+
}
155+
}
156+
}
157+
return true;
158+
}
159+
```
160+
161+
#### Rust
162+
163+
```rust
164+
impl Solution {
165+
pub fn is_toeplitz_matrix(matrix: Vec<Vec<i32>>) -> bool {
166+
let (m, n) = (matrix.len(), matrix[0].len());
167+
for i in 1..m {
168+
for j in 1..n {
169+
if matrix[i][j] != matrix[i - 1][j - 1] {
170+
return false;
171+
}
172+
}
173+
}
174+
true
175+
}
176+
}
177+
```
178+
145179
#### JavaScript
146180

147181
```js
@@ -150,11 +184,10 @@ func isToeplitzMatrix(matrix [][]int) bool {
150184
* @return {boolean}
151185
*/
152186
var isToeplitzMatrix = function (matrix) {
153-
const m = matrix.length;
154-
const n = matrix[0].length;
187+
const [m, n] = [matrix.length, matrix[0].length];
155188
for (let i = 1; i < m; ++i) {
156189
for (let j = 1; j < n; ++j) {
157-
if (matrix[i][j] != matrix[i - 1][j - 1]) {
190+
if (matrix[i][j] !== matrix[i - 1][j - 1]) {
158191
return false;
159192
}
160193
}

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

+46-9
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ The diagonal &quot;[1, 2]&quot; has different elements.
6666

6767
<!-- solution:start -->
6868

69-
### Solution 1
69+
### Solution 1: Single Traversal
70+
71+
According to the problem description, the characteristic of a Toeplitz matrix is that each element is equal to the element in its upper left corner. Therefore, we only need to iterate through each element in the matrix and check if it is equal to the element in its upper left corner.
72+
73+
The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$.
7074

7175
<!-- tabs:start -->
7276

@@ -76,11 +80,11 @@ The diagonal &quot;[1, 2]&quot; has different elements.
7680
class Solution:
7781
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
7882
m, n = len(matrix), len(matrix[0])
79-
return all(
80-
matrix[i][j] == matrix[i - 1][j - 1]
81-
for i in range(1, m)
82-
for j in range(1, n)
83-
)
83+
for i in range(1, m):
84+
for j in range(1, n):
85+
if matrix[i][j] != matrix[i - 1][j - 1]:
86+
return False
87+
return True
8488
```
8589

8690
#### Java
@@ -136,6 +140,40 @@ func isToeplitzMatrix(matrix [][]int) bool {
136140
}
137141
```
138142

143+
#### TypeScript
144+
145+
```ts
146+
function isToeplitzMatrix(matrix: number[][]): boolean {
147+
const [m, n] = [matrix.length, matrix[0].length];
148+
for (let i = 1; i < m; ++i) {
149+
for (let j = 1; j < n; ++j) {
150+
if (matrix[i][j] !== matrix[i - 1][j - 1]) {
151+
return false;
152+
}
153+
}
154+
}
155+
return true;
156+
}
157+
```
158+
159+
#### Rust
160+
161+
```rust
162+
impl Solution {
163+
pub fn is_toeplitz_matrix(matrix: Vec<Vec<i32>>) -> bool {
164+
let (m, n) = (matrix.len(), matrix[0].len());
165+
for i in 1..m {
166+
for j in 1..n {
167+
if matrix[i][j] != matrix[i - 1][j - 1] {
168+
return false;
169+
}
170+
}
171+
}
172+
true
173+
}
174+
}
175+
```
176+
139177
#### JavaScript
140178

141179
```js
@@ -144,11 +182,10 @@ func isToeplitzMatrix(matrix [][]int) bool {
144182
* @return {boolean}
145183
*/
146184
var isToeplitzMatrix = function (matrix) {
147-
const m = matrix.length;
148-
const n = matrix[0].length;
185+
const [m, n] = [matrix.length, matrix[0].length];
149186
for (let i = 1; i < m; ++i) {
150187
for (let j = 1; j < n; ++j) {
151-
if (matrix[i][j] != matrix[i - 1][j - 1]) {
188+
if (matrix[i][j] !== matrix[i - 1][j - 1]) {
152189
return false;
153190
}
154191
}

solution/0700-0799/0766.Toeplitz Matrix/Solution.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
* @return {boolean}
44
*/
55
var isToeplitzMatrix = function (matrix) {
6-
const m = matrix.length;
7-
const n = matrix[0].length;
6+
const [m, n] = [matrix.length, matrix[0].length];
87
for (let i = 1; i < m; ++i) {
98
for (let j = 1; j < n; ++j) {
10-
if (matrix[i][j] != matrix[i - 1][j - 1]) {
9+
if (matrix[i][j] !== matrix[i - 1][j - 1]) {
1110
return false;
1211
}
1312
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution:
22
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
33
m, n = len(matrix), len(matrix[0])
4-
return all(
5-
matrix[i][j] == matrix[i - 1][j - 1]
6-
for i in range(1, m)
7-
for j in range(1, n)
8-
)
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn is_toeplitz_matrix(matrix: Vec<Vec<i32>>) -> bool {
3+
let (m, n) = (matrix.len(), matrix[0].len());
4+
for i in 1..m {
5+
for j in 1..n {
6+
if matrix[i][j] != matrix[i - 1][j - 1] {
7+
return false;
8+
}
9+
}
10+
}
11+
true
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function isToeplitzMatrix(matrix: number[][]): boolean {
2+
const [m, n] = [matrix.length, matrix[0].length];
3+
for (let i = 1; i < m; ++i) {
4+
for (let j = 1; j < n; ++j) {
5+
if (matrix[i][j] !== matrix[i - 1][j - 1]) {
6+
return false;
7+
}
8+
}
9+
}
10+
return true;
11+
}

0 commit comments

Comments
 (0)