Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.0766 #4157

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 45 additions & 12 deletions solution/0700-0799/0766.Toeplitz Matrix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ tags:
<strong>输入:</strong>matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
<strong>输出:</strong>true
<strong>解释:</strong>
在上述矩阵中, 其对角线为:
"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]"。
在上述矩阵中, 其对角线为:
"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]"。
各条对角线上的所有元素均相同, 因此答案是 True 。
</pre>

Expand Down Expand Up @@ -70,9 +70,9 @@ tags:

### 方法一:一次遍历

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

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

<!-- tabs:start -->

Expand All @@ -82,11 +82,11 @@ tags:
class Solution:
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
m, n = len(matrix), len(matrix[0])
return all(
matrix[i][j] == matrix[i - 1][j - 1]
for i in range(1, m)
for j in range(1, n)
)
for i in range(1, m):
for j in range(1, n):
if matrix[i][j] != matrix[i - 1][j - 1]:
return False
return True
```

#### Java
Expand Down Expand Up @@ -142,6 +142,40 @@ func isToeplitzMatrix(matrix [][]int) bool {
}
```

#### TypeScript

```ts
function isToeplitzMatrix(matrix: number[][]): boolean {
const [m, n] = [matrix.length, matrix[0].length];
for (let i = 1; i < m; ++i) {
for (let j = 1; j < n; ++j) {
if (matrix[i][j] !== matrix[i - 1][j - 1]) {
return false;
}
}
}
return true;
}
```

#### Rust

```rust
impl Solution {
pub fn is_toeplitz_matrix(matrix: Vec<Vec<i32>>) -> bool {
let (m, n) = (matrix.len(), matrix[0].len());
for i in 1..m {
for j in 1..n {
if matrix[i][j] != matrix[i - 1][j - 1] {
return false;
}
}
}
true
}
}
```

#### JavaScript

```js
Expand All @@ -150,11 +184,10 @@ func isToeplitzMatrix(matrix [][]int) bool {
* @return {boolean}
*/
var isToeplitzMatrix = function (matrix) {
const m = matrix.length;
const n = matrix[0].length;
const [m, n] = [matrix.length, matrix[0].length];
for (let i = 1; i < m; ++i) {
for (let j = 1; j < n; ++j) {
if (matrix[i][j] != matrix[i - 1][j - 1]) {
if (matrix[i][j] !== matrix[i - 1][j - 1]) {
return false;
}
}
Expand Down
55 changes: 46 additions & 9 deletions solution/0700-0799/0766.Toeplitz Matrix/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ The diagonal &quot;[1, 2]&quot; has different elements.

<!-- solution:start -->

### Solution 1
### Solution 1: Single Traversal

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.

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)$.

<!-- tabs:start -->

Expand All @@ -76,11 +80,11 @@ The diagonal &quot;[1, 2]&quot; has different elements.
class Solution:
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
m, n = len(matrix), len(matrix[0])
return all(
matrix[i][j] == matrix[i - 1][j - 1]
for i in range(1, m)
for j in range(1, n)
)
for i in range(1, m):
for j in range(1, n):
if matrix[i][j] != matrix[i - 1][j - 1]:
return False
return True
```

#### Java
Expand Down Expand Up @@ -136,6 +140,40 @@ func isToeplitzMatrix(matrix [][]int) bool {
}
```

#### TypeScript

```ts
function isToeplitzMatrix(matrix: number[][]): boolean {
const [m, n] = [matrix.length, matrix[0].length];
for (let i = 1; i < m; ++i) {
for (let j = 1; j < n; ++j) {
if (matrix[i][j] !== matrix[i - 1][j - 1]) {
return false;
}
}
}
return true;
}
```

#### Rust

```rust
impl Solution {
pub fn is_toeplitz_matrix(matrix: Vec<Vec<i32>>) -> bool {
let (m, n) = (matrix.len(), matrix[0].len());
for i in 1..m {
for j in 1..n {
if matrix[i][j] != matrix[i - 1][j - 1] {
return false;
}
}
}
true
}
}
```

#### JavaScript

```js
Expand All @@ -144,11 +182,10 @@ func isToeplitzMatrix(matrix [][]int) bool {
* @return {boolean}
*/
var isToeplitzMatrix = function (matrix) {
const m = matrix.length;
const n = matrix[0].length;
const [m, n] = [matrix.length, matrix[0].length];
for (let i = 1; i < m; ++i) {
for (let j = 1; j < n; ++j) {
if (matrix[i][j] != matrix[i - 1][j - 1]) {
if (matrix[i][j] !== matrix[i - 1][j - 1]) {
return false;
}
}
Expand Down
5 changes: 2 additions & 3 deletions solution/0700-0799/0766.Toeplitz Matrix/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
* @return {boolean}
*/
var isToeplitzMatrix = function (matrix) {
const m = matrix.length;
const n = matrix[0].length;
const [m, n] = [matrix.length, matrix[0].length];
for (let i = 1; i < m; ++i) {
for (let j = 1; j < n; ++j) {
if (matrix[i][j] != matrix[i - 1][j - 1]) {
if (matrix[i][j] !== matrix[i - 1][j - 1]) {
return false;
}
}
Expand Down
10 changes: 5 additions & 5 deletions solution/0700-0799/0766.Toeplitz Matrix/Solution.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Solution:
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
m, n = len(matrix), len(matrix[0])
return all(
matrix[i][j] == matrix[i - 1][j - 1]
for i in range(1, m)
for j in range(1, n)
)
for i in range(1, m):
for j in range(1, n):
if matrix[i][j] != matrix[i - 1][j - 1]:
return False
return True
13 changes: 13 additions & 0 deletions solution/0700-0799/0766.Toeplitz Matrix/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
impl Solution {
pub fn is_toeplitz_matrix(matrix: Vec<Vec<i32>>) -> bool {
let (m, n) = (matrix.len(), matrix[0].len());
for i in 1..m {
for j in 1..n {
if matrix[i][j] != matrix[i - 1][j - 1] {
return false;
}
}
}
true
}
}
11 changes: 11 additions & 0 deletions solution/0700-0799/0766.Toeplitz Matrix/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function isToeplitzMatrix(matrix: number[][]): boolean {
const [m, n] = [matrix.length, matrix[0].length];
for (let i = 1; i < m; ++i) {
for (let j = 1; j < n; ++j) {
if (matrix[i][j] !== matrix[i - 1][j - 1]) {
return false;
}
}
}
return true;
}