From 86f6f28cb108d0aed7b146adcf23b33e05b9d3c1 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 13 Mar 2025 15:03:42 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.0766 No.0766.Toeplitz Matrix --- .../0700-0799/0766.Toeplitz Matrix/README.md | 57 +++++++++++++++---- .../0766.Toeplitz Matrix/README_EN.md | 55 +++++++++++++++--- .../0766.Toeplitz Matrix/Solution.js | 5 +- .../0766.Toeplitz Matrix/Solution.py | 10 ++-- .../0766.Toeplitz Matrix/Solution.rs | 13 +++++ .../0766.Toeplitz Matrix/Solution.ts | 11 ++++ 6 files changed, 122 insertions(+), 29 deletions(-) create mode 100644 solution/0700-0799/0766.Toeplitz Matrix/Solution.rs create mode 100644 solution/0700-0799/0766.Toeplitz Matrix/Solution.ts diff --git a/solution/0700-0799/0766.Toeplitz Matrix/README.md b/solution/0700-0799/0766.Toeplitz Matrix/README.md index 8b7e247b86c0c..7912a00f976c1 100644 --- a/solution/0700-0799/0766.Toeplitz Matrix/README.md +++ b/solution/0700-0799/0766.Toeplitz Matrix/README.md @@ -29,8 +29,8 @@ tags: 输入:matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]] 输出:true 解释: -在上述矩阵中, 其对角线为: -"[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 。 @@ -70,9 +70,9 @@ tags: ### 方法一:一次遍历 -遍历矩阵,若出现元素与其左上角的元素不等的情况,返回 `false`。否则,遍历结束后返回 `true`。 +根据题目描述,托普利茨矩阵的特点是:矩阵中每个元素都与其左上角的元素相等。因此,我们只需要遍历矩阵中的每个元素,检查它是否与左上角的元素相等即可。 -时间复杂度 $O(m \times n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。 +时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$。 @@ -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 @@ -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>) -> 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 @@ -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; } } diff --git a/solution/0700-0799/0766.Toeplitz Matrix/README_EN.md b/solution/0700-0799/0766.Toeplitz Matrix/README_EN.md index 0de95666d2991..269acd9ecdaaa 100644 --- a/solution/0700-0799/0766.Toeplitz Matrix/README_EN.md +++ b/solution/0700-0799/0766.Toeplitz Matrix/README_EN.md @@ -66,7 +66,11 @@ The diagonal "[1, 2]" has different elements. -### 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)$. @@ -76,11 +80,11 @@ The diagonal "[1, 2]" 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 @@ -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>) -> 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 @@ -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; } } diff --git a/solution/0700-0799/0766.Toeplitz Matrix/Solution.js b/solution/0700-0799/0766.Toeplitz Matrix/Solution.js index 7b23d487032ef..81cab524dde42 100644 --- a/solution/0700-0799/0766.Toeplitz Matrix/Solution.js +++ b/solution/0700-0799/0766.Toeplitz Matrix/Solution.js @@ -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; } } diff --git a/solution/0700-0799/0766.Toeplitz Matrix/Solution.py b/solution/0700-0799/0766.Toeplitz Matrix/Solution.py index 53f7c0f033a66..4f5f378d23848 100644 --- a/solution/0700-0799/0766.Toeplitz Matrix/Solution.py +++ b/solution/0700-0799/0766.Toeplitz Matrix/Solution.py @@ -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 diff --git a/solution/0700-0799/0766.Toeplitz Matrix/Solution.rs b/solution/0700-0799/0766.Toeplitz Matrix/Solution.rs new file mode 100644 index 0000000000000..f033d3e195634 --- /dev/null +++ b/solution/0700-0799/0766.Toeplitz Matrix/Solution.rs @@ -0,0 +1,13 @@ +impl Solution { + pub fn is_toeplitz_matrix(matrix: Vec>) -> 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 + } +} diff --git a/solution/0700-0799/0766.Toeplitz Matrix/Solution.ts b/solution/0700-0799/0766.Toeplitz Matrix/Solution.ts new file mode 100644 index 0000000000000..cbe193e8f0ff7 --- /dev/null +++ b/solution/0700-0799/0766.Toeplitz Matrix/Solution.ts @@ -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; +}