diff --git a/solution/0000-0099/0054.Spiral Matrix/README.md b/solution/0000-0099/0054.Spiral Matrix/README.md index 8518d2f75f7cb..471bde2403299 100644 --- a/solution/0000-0099/0054.Spiral Matrix/README.md +++ b/solution/0000-0099/0054.Spiral Matrix/README.md @@ -55,12 +55,10 @@ tags: ### 方法一:模拟 -我们用 $i$ 和 $j$ 分别表示当前访问到的元素的行和列,用 $k$ 表示当前的方向,用数组或哈希表 $vis$ 记录每个元素是否被访问过。每次我们访问到一个元素后,将其标记为已访问,然后按照当前的方向前进一步,如果前进一步后发现越界或者已经访问过,则改变方向继续前进,直到遍历完整个矩阵。 +我们可以模拟整个遍历的过程,用 $i$ 和 $j$ 分别表示当前访问到的元素的行和列,用 $k$ 表示当前的方向,用数组或哈希表 $\textit{vis}$ 记录每个元素是否被访问过。每次我们访问到一个元素后,将其标记为已访问,然后按照当前的方向前进一步,如果前进一步后发现越界或者已经访问过,则改变方向继续前进,直到遍历完整个矩阵。 时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。 -对于访问过的元素,我们也可以将其值加上一个常数 $300$,这样就不需要额外的 $vis$ 数组或哈希表来记录是否访问过了,从而将空间复杂度降低到 $O(1)$。 - #### Python3 @@ -70,17 +68,17 @@ class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: m, n = len(matrix), len(matrix[0]) dirs = (0, 1, 0, -1, 0) + vis = [[False] * n for _ in range(m)] i = j = k = 0 ans = [] - vis = set() for _ in range(m * n): ans.append(matrix[i][j]) - vis.add((i, j)) + vis[i][j] = True x, y = i + dirs[k], j + dirs[k + 1] - if not 0 <= x < m or not 0 <= y < n or (x, y) in vis: + if x < 0 or x >= m or y < 0 or y >= n or vis[x][y]: k = (k + 1) % 4 - i = i + dirs[k] - j = j + dirs[k + 1] + i += dirs[k] + j += dirs[k + 1] return ans ``` @@ -167,7 +165,7 @@ function spiralOrder(matrix: number[][]): number[] { const m = matrix.length; const n = matrix[0].length; const ans: number[] = []; - const vis = new Array(m).fill(0).map(() => new Array(n).fill(false)); + const vis: boolean[][] = Array.from({ length: m }, () => Array(n).fill(false)); const dirs = [0, 1, 0, -1, 0]; for (let h = m * n, i = 0, j = 0, k = 0; h > 0; --h) { ans.push(matrix[i][j]); @@ -189,37 +187,30 @@ function spiralOrder(matrix: number[][]): number[] { ```rust impl Solution { pub fn spiral_order(matrix: Vec>) -> Vec { - let mut x1 = 0; - let mut y1 = 0; - let mut x2 = matrix.len() - 1; - let mut y2 = matrix[0].len() - 1; - let mut result = vec![]; - - while x1 <= x2 && y1 <= y2 { - for j in y1..=y2 { - result.push(matrix[x1][j]); - } - for i in x1 + 1..=x2 { - result.push(matrix[i][y2]); - } - if x1 < x2 && y1 < y2 { - for j in (y1..y2).rev() { - result.push(matrix[x2][j]); - } - for i in (x1 + 1..x2).rev() { - result.push(matrix[i][y1]); - } - } - x1 += 1; - y1 += 1; - if x2 != 0 { - x2 -= 1; - } - if y2 != 0 { - y2 -= 1; + let m = matrix.len(); + let n = matrix[0].len(); + let mut dirs = vec![0, 1, 0, -1, 0]; + let mut vis = vec![vec![false; n]; m]; + let mut i = 0; + let mut j = 0; + let mut k = 0; + let mut ans = Vec::new(); + + for _ in 0..(m * n) { + ans.push(matrix[i][j]); + vis[i][j] = true; + let x = i as i32 + dirs[k] as i32; + let y = j as i32 + dirs[k + 1] as i32; + + if x < 0 || x >= m as i32 || y < 0 || y >= n as i32 || vis[x as usize][y as usize] { + k = (k + 1) % 4; } + + i = (i as i32 + dirs[k] as i32) as usize; + j = (j as i32 + dirs[k + 1] as i32) as usize; } - return result; + + ans } } ``` @@ -235,7 +226,7 @@ var spiralOrder = function (matrix) { const m = matrix.length; const n = matrix[0].length; const ans = []; - const vis = new Array(m).fill(0).map(() => new Array(n).fill(false)); + const vis = Array.from({ length: m }, () => Array(n).fill(false)); const dirs = [0, 1, 0, -1, 0]; for (let h = m * n, i = 0, j = 0, k = 0; h > 0; --h) { ans.push(matrix[i][j]); @@ -258,14 +249,15 @@ var spiralOrder = function (matrix) { public class Solution { public IList SpiralOrder(int[][] matrix) { int m = matrix.Length, n = matrix[0].Length; - int[] dirs = new int[] {0, 1, 0, -1, 0}; + int[] dirs = { 0, 1, 0, -1, 0 }; + int i = 0, j = 0, k = 0; IList ans = new List(); - bool[,] visited = new bool[m, n]; - for (int h = m * n, i = 0, j = 0, k = 0; h > 0; --h) { + bool[,] vis = new bool[m, n]; + for (int h = m * n; h > 0; --h) { ans.Add(matrix[i][j]); - visited[i, j] = true; + vis[i, j] = true; int x = i + dirs[k], y = j + dirs[k + 1]; - if (x < 0 || x >= m || y < 0 || y >= n || visited[x, y]) { + if (x < 0 || x >= m || y < 0 || y >= n || vis[x, y]) { k = (k + 1) % 4; } i += dirs[k]; @@ -282,11 +274,11 @@ public class Solution { -### 方法二:逐层模拟 +### 方法二:模拟(空间优化) -我们也可以从外往里一圈一圈遍历并存储矩阵元素。 +注意到,题目中矩阵元素取值范围为 $[-100, 100]$,因此,我们可以将访问过的元素加上一个较大的值,比如 $300$,这样只需要判断访问的元素是否大于 $100$ 即可,无需额外的空间记录是否访问过。如果最终需要将访问过的元素恢复原值,可以在遍历结束后再次遍历一遍矩阵,将所有元素减去 $300$。 -时间复杂度 $O(m \times n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。 +时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$。 @@ -303,13 +295,13 @@ class Solution: ans.append(matrix[i][j]) matrix[i][j] += 300 x, y = i + dirs[k], j + dirs[k + 1] - if not 0 <= x < m or not 0 <= y < n or matrix[x][y] > 100: + if x < 0 or x >= m or y < 0 or y >= n or matrix[x][y] > 100: k = (k + 1) % 4 - i = i + dirs[k] - j = j + dirs[k + 1] - # for i in range(m): - # for j in range(n): - # matrix[i][j] -= 300 + i += dirs[k] + j += dirs[k + 1] + for i in range(m): + for j in range(n): + matrix[i][j] -= 300 return ans ``` @@ -320,8 +312,9 @@ class Solution { public List spiralOrder(int[][] matrix) { int m = matrix.length, n = matrix[0].length; int[] dirs = {0, 1, 0, -1, 0}; + int i = 0, j = 0, k = 0; List ans = new ArrayList<>(); - for (int h = m * n, i = 0, j = 0, k = 0; h > 0; --h) { + for (int h = m * n; h > 0; --h) { ans.add(matrix[i][j]); matrix[i][j] += 300; int x = i + dirs[k], y = j + dirs[k + 1]; @@ -331,11 +324,11 @@ class Solution { i += dirs[k]; j += dirs[k + 1]; } - // for (int i = 0; i < m; ++i) { - // for (int j = 0; j < n; ++j) { - // matrix[i][j] -= 300; - // } - // } + for (i = 0; i < m; ++i) { + for (j = 0; j < n; ++j) { + matrix[i][j] -= 300; + } + } return ans; } } @@ -349,8 +342,9 @@ public: vector spiralOrder(vector>& matrix) { int m = matrix.size(), n = matrix[0].size(); int dirs[5] = {0, 1, 0, -1, 0}; + int i = 0, j = 0, k = 0; vector ans; - for (int h = m * n, i = 0, j = 0, k = 0; h; --h) { + for (int h = m * n; h; --h) { ans.push_back(matrix[i][j]); matrix[i][j] += 300; int x = i + dirs[k], y = j + dirs[k + 1]; @@ -360,11 +354,11 @@ public: i += dirs[k]; j += dirs[k + 1]; } - // for (int i = 0; i < m; ++i) { - // for (int j = 0; j < n; ++j) { - // matrix[i][j] -= 300; - // } - // } + for (i = 0; i < m; ++i) { + for (j = 0; j < n; ++j) { + matrix[i][j] -= 300; + } + } return ans; } }; @@ -376,7 +370,8 @@ public: func spiralOrder(matrix [][]int) (ans []int) { m, n := len(matrix), len(matrix[0]) dirs := [5]int{0, 1, 0, -1, 0} - for h, i, j, k := m*n, 0, 0, 0; h > 0; h-- { + i, j, k := 0, 0, 0 + for h := m * n; h > 0; h-- { ans = append(ans, matrix[i][j]) matrix[i][j] += 300 x, y := i+dirs[k], j+dirs[k+1] @@ -385,11 +380,11 @@ func spiralOrder(matrix [][]int) (ans []int) { } i, j = i+dirs[k], j+dirs[k+1] } - // for i, row := range matrix { - // for j := range row { - // matrix[i][j] -= 300 - // } - // } + for i = 0; i < m; i++ { + for j = 0; j < n; j++ { + matrix[i][j] -= 300 + } + } return } ``` @@ -413,15 +408,58 @@ function spiralOrder(matrix: number[][]): number[] { i += dirs[k]; j += dirs[k + 1]; } - // for (let i = 0; i < m; ++i) { - // for (let j = 0; j < n; ++j) { - // matrix[i][j] -= 300; - // } - // } + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; ++j) { + matrix[i][j] -= 300; + } + } return ans; } ``` +#### Rust + +```rust +impl Solution { + pub fn spiral_order(mut matrix: Vec>) -> Vec { + let m = matrix.len(); + let n = matrix[0].len(); + let mut dirs = vec![0, 1, 0, -1, 0]; + let mut i = 0; + let mut j = 0; + let mut k = 0; + let mut ans = Vec::new(); + + for _ in 0..(m * n) { + ans.push(matrix[i][j]); + matrix[i][j] += 300; + let x = i as i32 + dirs[k] as i32; + let y = j as i32 + dirs[k + 1] as i32; + + if x < 0 + || x >= m as i32 + || y < 0 + || y >= n as i32 + || matrix[x as usize][y as usize] > 100 + { + k = (k + 1) % 4; + } + + i = (i as i32 + dirs[k] as i32) as usize; + j = (j as i32 + dirs[k + 1] as i32) as usize; + } + + for i in 0..m { + for j in 0..n { + matrix[i][j] -= 300; + } + } + + ans + } +} +``` + #### JavaScript ```js @@ -445,11 +483,11 @@ var spiralOrder = function (matrix) { i += dirs[k]; j += dirs[k + 1]; } - // for (let i = 0; i < m; ++i) { - // for (let j = 0; j < n; ++j) { - // matrix[i][j] -= 300; - // } - // } + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; ++j) { + matrix[i][j] -= 300; + } + } return ans; }; ``` @@ -460,9 +498,10 @@ var spiralOrder = function (matrix) { public class Solution { public IList SpiralOrder(int[][] matrix) { int m = matrix.Length, n = matrix[0].Length; - int[] dirs = new int[] {0, 1, 0, -1, 0}; + int[] dirs = { 0, 1, 0, -1, 0 }; + int i = 0, j = 0, k = 0; IList ans = new List(); - for (int h = m * n, i = 0, j = 0, k = 0; h > 0; --h) { + for (int h = m * n; h > 0; --h) { ans.Add(matrix[i][j]); matrix[i][j] += 300; int x = i + dirs[k], y = j + dirs[k + 1]; @@ -472,243 +511,10 @@ public class Solution { i += dirs[k]; j += dirs[k + 1]; } - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - matrix[i][j] -= 300; - } - } - return ans; - } -} -``` - - - - - - - -### 方法三 - - - -#### Python3 - -```python -class Solution: - def spiralOrder(self, matrix: List[List[int]]) -> List[int]: - m, n = len(matrix), len(matrix[0]) - x1, y1, x2, y2 = 0, 0, m - 1, n - 1 - ans = [] - while x1 <= x2 and y1 <= y2: - for j in range(y1, y2 + 1): - ans.append(matrix[x1][j]) - for i in range(x1 + 1, x2 + 1): - ans.append(matrix[i][y2]) - if x1 < x2 and y1 < y2: - for j in range(y2 - 1, y1 - 1, -1): - ans.append(matrix[x2][j]) - for i in range(x2 - 1, x1, -1): - ans.append(matrix[i][y1]) - x1, y1 = x1 + 1, y1 + 1 - x2, y2 = x2 - 1, y2 - 1 - return ans -``` - -#### Java - -```java -class Solution { - public List spiralOrder(int[][] matrix) { - int m = matrix.length, n = matrix[0].length; - int x1 = 0, y1 = 0, x2 = m - 1, y2 = n - 1; - List ans = new ArrayList<>(); - while (x1 <= x2 && y1 <= y2) { - for (int j = y1; j <= y2; ++j) { - ans.add(matrix[x1][j]); - } - for (int i = x1 + 1; i <= x2; ++i) { - ans.add(matrix[i][y2]); - } - if (x1 < x2 && y1 < y2) { - for (int j = y2 - 1; j >= y1; --j) { - ans.add(matrix[x2][j]); - } - for (int i = x2 - 1; i > x1; --i) { - ans.add(matrix[i][y1]); - } - } - ++x1; - ++y1; - --x2; - --y2; - } - return ans; - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - vector spiralOrder(vector>& matrix) { - int m = matrix.size(), n = matrix[0].size(); - int x1 = 0, y1 = 0, x2 = m - 1, y2 = n - 1; - vector ans; - while (x1 <= x2 && y1 <= y2) { - for (int j = y1; j <= y2; ++j) { - ans.push_back(matrix[x1][j]); - } - for (int i = x1 + 1; i <= x2; ++i) { - ans.push_back(matrix[i][y2]); - } - if (x1 < x2 && y1 < y2) { - for (int j = y2 - 1; j >= y1; --j) { - ans.push_back(matrix[x2][j]); - } - for (int i = x2 - 1; i > x1; --i) { - ans.push_back(matrix[i][y1]); - } - } - ++x1, ++y1; - --x2, --y2; - } - return ans; - } -}; -``` - -#### Go - -```go -func spiralOrder(matrix [][]int) (ans []int) { - m, n := len(matrix), len(matrix[0]) - x1, y1, x2, y2 := 0, 0, m-1, n-1 - for x1 <= x2 && y1 <= y2 { - for j := y1; j <= y2; j++ { - ans = append(ans, matrix[x1][j]) - } - for i := x1 + 1; i <= x2; i++ { - ans = append(ans, matrix[i][y2]) - } - if x1 < x2 && y1 < y2 { - for j := y2 - 1; j >= y1; j-- { - ans = append(ans, matrix[x2][j]) - } - for i := x2 - 1; i > x1; i-- { - ans = append(ans, matrix[i][y1]) - } - } - x1, y1 = x1+1, y1+1 - x2, y2 = x2-1, y2-1 - } - return -} -``` - -#### TypeScript - -```ts -function spiralOrder(matrix: number[][]): number[] { - const m = matrix.length; - const n = matrix[0].length; - let x1 = 0; - let y1 = 0; - let x2 = m - 1; - let y2 = n - 1; - const ans: number[] = []; - while (x1 <= x2 && y1 <= y2) { - for (let j = y1; j <= y2; ++j) { - ans.push(matrix[x1][j]); - } - for (let i = x1 + 1; i <= x2; ++i) { - ans.push(matrix[i][y2]); - } - if (x1 < x2 && y1 < y2) { - for (let j = y2 - 1; j >= y1; --j) { - ans.push(matrix[x2][j]); - } - for (let i = x2 - 1; i > x1; --i) { - ans.push(matrix[i][y1]); - } - } - ++x1; - ++y1; - --x2; - --y2; - } - return ans; -} -``` - -#### JavaScript - -```js -/** - * @param {number[][]} matrix - * @return {number[]} - */ -var spiralOrder = function (matrix) { - const m = matrix.length; - const n = matrix[0].length; - let x1 = 0; - let y1 = 0; - let x2 = m - 1; - let y2 = n - 1; - const ans = []; - while (x1 <= x2 && y1 <= y2) { - for (let j = y1; j <= y2; ++j) { - ans.push(matrix[x1][j]); - } - for (let i = x1 + 1; i <= x2; ++i) { - ans.push(matrix[i][y2]); - } - if (x1 < x2 && y1 < y2) { - for (let j = y2 - 1; j >= y1; --j) { - ans.push(matrix[x2][j]); - } - for (let i = x2 - 1; i > x1; --i) { - ans.push(matrix[i][y1]); - } - } - ++x1; - ++y1; - --x2; - --y2; - } - return ans; -}; -``` - -#### C# - -```cs -public class Solution { - public IList SpiralOrder(int[][] matrix) { - int m = matrix.Length, n = matrix[0].Length; - int x1 = 0, y1 = 0, x2 = m - 1, y2 = n - 1; - IList ans = new List(); - while (x1 <= x2 && y1 <= y2) { - for (int j = y1; j <= y2; ++j) { - ans.Add(matrix[x1][j]); - } - for (int i = x1 + 1; i <= x2; ++i) { - ans.Add(matrix[i][y2]); - } - if (x1 < x2 && y1 < y2) { - for (int j = y2 - 1; j >= y1; --j) { - ans.Add(matrix[x2][j]); - } - for (int i = x2 - 1; i > x1; --i) { - ans.Add(matrix[i][y1]); - } + for (int a = 0; a < m; ++a) { + for (int b = 0; b < n; ++b) { + matrix[a][b] -= 300; } - ++x1; - ++y1; - --x2; - --y2; } return ans; } diff --git a/solution/0000-0099/0054.Spiral Matrix/README_EN.md b/solution/0000-0099/0054.Spiral Matrix/README_EN.md index 4cd50bbb1e344..1bfd4e609d51a 100644 --- a/solution/0000-0099/0054.Spiral Matrix/README_EN.md +++ b/solution/0000-0099/0054.Spiral Matrix/README_EN.md @@ -53,12 +53,10 @@ tags: ### Solution 1: Simulation -We use $i$ and $j$ to represent the row and column of the current element, use $k$ to represent the current direction, and use an array or hash table $vis$ to record whether each element has been visited. Each time we visit an element, we mark it as visited, then move forward in the current direction. If we find that it is out of bounds or has been visited after moving forward, we change the direction and continue to move forward until the entire matrix is traversed. +We can simulate the entire traversal process. We use $i$ and $j$ to represent the row and column of the current element being visited, and $k$ to represent the current direction. We use an array or hash table $\textit{vis}$ to record whether each element has been visited. Each time we visit an element, we mark it as visited, then move one step forward in the current direction. If moving forward results in an out-of-bounds condition or the element has already been visited, we change direction and continue moving forward until the entire matrix has been traversed. The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the number of rows and columns of the matrix, respectively. -For visited elements, we can also add a constant $300$ to their values, so we don't need an extra $vis$ array or hash table to record whether they have been visited, thereby reducing the space complexity to $O(1)$. - #### Python3 @@ -68,17 +66,17 @@ class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: m, n = len(matrix), len(matrix[0]) dirs = (0, 1, 0, -1, 0) + vis = [[False] * n for _ in range(m)] i = j = k = 0 ans = [] - vis = set() for _ in range(m * n): ans.append(matrix[i][j]) - vis.add((i, j)) + vis[i][j] = True x, y = i + dirs[k], j + dirs[k + 1] - if not 0 <= x < m or not 0 <= y < n or (x, y) in vis: + if x < 0 or x >= m or y < 0 or y >= n or vis[x][y]: k = (k + 1) % 4 - i = i + dirs[k] - j = j + dirs[k + 1] + i += dirs[k] + j += dirs[k + 1] return ans ``` @@ -165,7 +163,7 @@ function spiralOrder(matrix: number[][]): number[] { const m = matrix.length; const n = matrix[0].length; const ans: number[] = []; - const vis = new Array(m).fill(0).map(() => new Array(n).fill(false)); + const vis: boolean[][] = Array.from({ length: m }, () => Array(n).fill(false)); const dirs = [0, 1, 0, -1, 0]; for (let h = m * n, i = 0, j = 0, k = 0; h > 0; --h) { ans.push(matrix[i][j]); @@ -187,37 +185,30 @@ function spiralOrder(matrix: number[][]): number[] { ```rust impl Solution { pub fn spiral_order(matrix: Vec>) -> Vec { - let mut x1 = 0; - let mut y1 = 0; - let mut x2 = matrix.len() - 1; - let mut y2 = matrix[0].len() - 1; - let mut result = vec![]; - - while x1 <= x2 && y1 <= y2 { - for j in y1..=y2 { - result.push(matrix[x1][j]); - } - for i in x1 + 1..=x2 { - result.push(matrix[i][y2]); - } - if x1 < x2 && y1 < y2 { - for j in (y1..y2).rev() { - result.push(matrix[x2][j]); - } - for i in (x1 + 1..x2).rev() { - result.push(matrix[i][y1]); - } - } - x1 += 1; - y1 += 1; - if x2 != 0 { - x2 -= 1; - } - if y2 != 0 { - y2 -= 1; + let m = matrix.len(); + let n = matrix[0].len(); + let mut dirs = vec![0, 1, 0, -1, 0]; + let mut vis = vec![vec![false; n]; m]; + let mut i = 0; + let mut j = 0; + let mut k = 0; + let mut ans = Vec::new(); + + for _ in 0..(m * n) { + ans.push(matrix[i][j]); + vis[i][j] = true; + let x = i as i32 + dirs[k] as i32; + let y = j as i32 + dirs[k + 1] as i32; + + if x < 0 || x >= m as i32 || y < 0 || y >= n as i32 || vis[x as usize][y as usize] { + k = (k + 1) % 4; } + + i = (i as i32 + dirs[k] as i32) as usize; + j = (j as i32 + dirs[k + 1] as i32) as usize; } - return result; + + ans } } ``` @@ -233,7 +224,7 @@ var spiralOrder = function (matrix) { const m = matrix.length; const n = matrix[0].length; const ans = []; - const vis = new Array(m).fill(0).map(() => new Array(n).fill(false)); + const vis = Array.from({ length: m }, () => Array(n).fill(false)); const dirs = [0, 1, 0, -1, 0]; for (let h = m * n, i = 0, j = 0, k = 0; h > 0; --h) { ans.push(matrix[i][j]); @@ -256,14 +247,15 @@ var spiralOrder = function (matrix) { public class Solution { public IList SpiralOrder(int[][] matrix) { int m = matrix.Length, n = matrix[0].Length; - int[] dirs = new int[] {0, 1, 0, -1, 0}; + int[] dirs = { 0, 1, 0, -1, 0 }; + int i = 0, j = 0, k = 0; IList ans = new List(); - bool[,] visited = new bool[m, n]; - for (int h = m * n, i = 0, j = 0, k = 0; h > 0; --h) { + bool[,] vis = new bool[m, n]; + for (int h = m * n; h > 0; --h) { ans.Add(matrix[i][j]); - visited[i, j] = true; + vis[i, j] = true; int x = i + dirs[k], y = j + dirs[k + 1]; - if (x < 0 || x >= m || y < 0 || y >= n || visited[x, y]) { + if (x < 0 || x >= m || y < 0 || y >= n || vis[x, y]) { k = (k + 1) % 4; } i += dirs[k]; @@ -280,11 +272,11 @@ public class Solution { -### Solution 2: Layer-by-layer Simulation +### Solution 2: Simulation (Space Optimization) -We can also traverse and store the matrix elements from the outside to the inside, layer by layer. +Notice that the range of matrix element values is $[-100, 100]$. Therefore, we can add a large value, such as $300$, to the visited elements. This way, we only need to check if the visited element is greater than $100$, without needing extra space to record whether it has been visited. If we need to restore the original values of the visited elements, we can traverse the matrix again after the traversal is complete and subtract $300$ from all elements. -The time complexity is $O(m \times n)$, and the space complexity is $O(1)$. Here, $m$ and $n$ are the number of rows and columns of the matrix, respectively. +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)$. @@ -301,13 +293,13 @@ class Solution: ans.append(matrix[i][j]) matrix[i][j] += 300 x, y = i + dirs[k], j + dirs[k + 1] - if not 0 <= x < m or not 0 <= y < n or matrix[x][y] > 100: + if x < 0 or x >= m or y < 0 or y >= n or matrix[x][y] > 100: k = (k + 1) % 4 - i = i + dirs[k] - j = j + dirs[k + 1] - # for i in range(m): - # for j in range(n): - # matrix[i][j] -= 300 + i += dirs[k] + j += dirs[k + 1] + for i in range(m): + for j in range(n): + matrix[i][j] -= 300 return ans ``` @@ -318,8 +310,9 @@ class Solution { public List spiralOrder(int[][] matrix) { int m = matrix.length, n = matrix[0].length; int[] dirs = {0, 1, 0, -1, 0}; + int i = 0, j = 0, k = 0; List ans = new ArrayList<>(); - for (int h = m * n, i = 0, j = 0, k = 0; h > 0; --h) { + for (int h = m * n; h > 0; --h) { ans.add(matrix[i][j]); matrix[i][j] += 300; int x = i + dirs[k], y = j + dirs[k + 1]; @@ -329,11 +322,11 @@ class Solution { i += dirs[k]; j += dirs[k + 1]; } - // for (int i = 0; i < m; ++i) { - // for (int j = 0; j < n; ++j) { - // matrix[i][j] -= 300; - // } - // } + for (i = 0; i < m; ++i) { + for (j = 0; j < n; ++j) { + matrix[i][j] -= 300; + } + } return ans; } } @@ -347,8 +340,9 @@ public: vector spiralOrder(vector>& matrix) { int m = matrix.size(), n = matrix[0].size(); int dirs[5] = {0, 1, 0, -1, 0}; + int i = 0, j = 0, k = 0; vector ans; - for (int h = m * n, i = 0, j = 0, k = 0; h; --h) { + for (int h = m * n; h; --h) { ans.push_back(matrix[i][j]); matrix[i][j] += 300; int x = i + dirs[k], y = j + dirs[k + 1]; @@ -358,11 +352,11 @@ public: i += dirs[k]; j += dirs[k + 1]; } - // for (int i = 0; i < m; ++i) { - // for (int j = 0; j < n; ++j) { - // matrix[i][j] -= 300; - // } - // } + for (i = 0; i < m; ++i) { + for (j = 0; j < n; ++j) { + matrix[i][j] -= 300; + } + } return ans; } }; @@ -374,7 +368,8 @@ public: func spiralOrder(matrix [][]int) (ans []int) { m, n := len(matrix), len(matrix[0]) dirs := [5]int{0, 1, 0, -1, 0} - for h, i, j, k := m*n, 0, 0, 0; h > 0; h-- { + i, j, k := 0, 0, 0 + for h := m * n; h > 0; h-- { ans = append(ans, matrix[i][j]) matrix[i][j] += 300 x, y := i+dirs[k], j+dirs[k+1] @@ -383,11 +378,11 @@ func spiralOrder(matrix [][]int) (ans []int) { } i, j = i+dirs[k], j+dirs[k+1] } - // for i, row := range matrix { - // for j := range row { - // matrix[i][j] -= 300 - // } - // } + for i = 0; i < m; i++ { + for j = 0; j < n; j++ { + matrix[i][j] -= 300 + } + } return } ``` @@ -411,15 +406,58 @@ function spiralOrder(matrix: number[][]): number[] { i += dirs[k]; j += dirs[k + 1]; } - // for (let i = 0; i < m; ++i) { - // for (let j = 0; j < n; ++j) { - // matrix[i][j] -= 300; - // } - // } + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; ++j) { + matrix[i][j] -= 300; + } + } return ans; } ``` +#### Rust + +```rust +impl Solution { + pub fn spiral_order(mut matrix: Vec>) -> Vec { + let m = matrix.len(); + let n = matrix[0].len(); + let mut dirs = vec![0, 1, 0, -1, 0]; + let mut i = 0; + let mut j = 0; + let mut k = 0; + let mut ans = Vec::new(); + + for _ in 0..(m * n) { + ans.push(matrix[i][j]); + matrix[i][j] += 300; + let x = i as i32 + dirs[k] as i32; + let y = j as i32 + dirs[k + 1] as i32; + + if x < 0 + || x >= m as i32 + || y < 0 + || y >= n as i32 + || matrix[x as usize][y as usize] > 100 + { + k = (k + 1) % 4; + } + + i = (i as i32 + dirs[k] as i32) as usize; + j = (j as i32 + dirs[k + 1] as i32) as usize; + } + + for i in 0..m { + for j in 0..n { + matrix[i][j] -= 300; + } + } + + ans + } +} +``` + #### JavaScript ```js @@ -443,11 +481,11 @@ var spiralOrder = function (matrix) { i += dirs[k]; j += dirs[k + 1]; } - // for (let i = 0; i < m; ++i) { - // for (let j = 0; j < n; ++j) { - // matrix[i][j] -= 300; - // } - // } + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; ++j) { + matrix[i][j] -= 300; + } + } return ans; }; ``` @@ -458,9 +496,10 @@ var spiralOrder = function (matrix) { public class Solution { public IList SpiralOrder(int[][] matrix) { int m = matrix.Length, n = matrix[0].Length; - int[] dirs = new int[] {0, 1, 0, -1, 0}; + int[] dirs = { 0, 1, 0, -1, 0 }; + int i = 0, j = 0, k = 0; IList ans = new List(); - for (int h = m * n, i = 0, j = 0, k = 0; h > 0; --h) { + for (int h = m * n; h > 0; --h) { ans.Add(matrix[i][j]); matrix[i][j] += 300; int x = i + dirs[k], y = j + dirs[k + 1]; @@ -470,243 +509,10 @@ public class Solution { i += dirs[k]; j += dirs[k + 1]; } - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - matrix[i][j] -= 300; - } - } - return ans; - } -} -``` - - - - - - - -### Solution 3 - - - -#### Python3 - -```python -class Solution: - def spiralOrder(self, matrix: List[List[int]]) -> List[int]: - m, n = len(matrix), len(matrix[0]) - x1, y1, x2, y2 = 0, 0, m - 1, n - 1 - ans = [] - while x1 <= x2 and y1 <= y2: - for j in range(y1, y2 + 1): - ans.append(matrix[x1][j]) - for i in range(x1 + 1, x2 + 1): - ans.append(matrix[i][y2]) - if x1 < x2 and y1 < y2: - for j in range(y2 - 1, y1 - 1, -1): - ans.append(matrix[x2][j]) - for i in range(x2 - 1, x1, -1): - ans.append(matrix[i][y1]) - x1, y1 = x1 + 1, y1 + 1 - x2, y2 = x2 - 1, y2 - 1 - return ans -``` - -#### Java - -```java -class Solution { - public List spiralOrder(int[][] matrix) { - int m = matrix.length, n = matrix[0].length; - int x1 = 0, y1 = 0, x2 = m - 1, y2 = n - 1; - List ans = new ArrayList<>(); - while (x1 <= x2 && y1 <= y2) { - for (int j = y1; j <= y2; ++j) { - ans.add(matrix[x1][j]); - } - for (int i = x1 + 1; i <= x2; ++i) { - ans.add(matrix[i][y2]); - } - if (x1 < x2 && y1 < y2) { - for (int j = y2 - 1; j >= y1; --j) { - ans.add(matrix[x2][j]); - } - for (int i = x2 - 1; i > x1; --i) { - ans.add(matrix[i][y1]); - } - } - ++x1; - ++y1; - --x2; - --y2; - } - return ans; - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - vector spiralOrder(vector>& matrix) { - int m = matrix.size(), n = matrix[0].size(); - int x1 = 0, y1 = 0, x2 = m - 1, y2 = n - 1; - vector ans; - while (x1 <= x2 && y1 <= y2) { - for (int j = y1; j <= y2; ++j) { - ans.push_back(matrix[x1][j]); - } - for (int i = x1 + 1; i <= x2; ++i) { - ans.push_back(matrix[i][y2]); - } - if (x1 < x2 && y1 < y2) { - for (int j = y2 - 1; j >= y1; --j) { - ans.push_back(matrix[x2][j]); - } - for (int i = x2 - 1; i > x1; --i) { - ans.push_back(matrix[i][y1]); - } - } - ++x1, ++y1; - --x2, --y2; - } - return ans; - } -}; -``` - -#### Go - -```go -func spiralOrder(matrix [][]int) (ans []int) { - m, n := len(matrix), len(matrix[0]) - x1, y1, x2, y2 := 0, 0, m-1, n-1 - for x1 <= x2 && y1 <= y2 { - for j := y1; j <= y2; j++ { - ans = append(ans, matrix[x1][j]) - } - for i := x1 + 1; i <= x2; i++ { - ans = append(ans, matrix[i][y2]) - } - if x1 < x2 && y1 < y2 { - for j := y2 - 1; j >= y1; j-- { - ans = append(ans, matrix[x2][j]) - } - for i := x2 - 1; i > x1; i-- { - ans = append(ans, matrix[i][y1]) - } - } - x1, y1 = x1+1, y1+1 - x2, y2 = x2-1, y2-1 - } - return -} -``` - -#### TypeScript - -```ts -function spiralOrder(matrix: number[][]): number[] { - const m = matrix.length; - const n = matrix[0].length; - let x1 = 0; - let y1 = 0; - let x2 = m - 1; - let y2 = n - 1; - const ans: number[] = []; - while (x1 <= x2 && y1 <= y2) { - for (let j = y1; j <= y2; ++j) { - ans.push(matrix[x1][j]); - } - for (let i = x1 + 1; i <= x2; ++i) { - ans.push(matrix[i][y2]); - } - if (x1 < x2 && y1 < y2) { - for (let j = y2 - 1; j >= y1; --j) { - ans.push(matrix[x2][j]); - } - for (let i = x2 - 1; i > x1; --i) { - ans.push(matrix[i][y1]); - } - } - ++x1; - ++y1; - --x2; - --y2; - } - return ans; -} -``` - -#### JavaScript - -```js -/** - * @param {number[][]} matrix - * @return {number[]} - */ -var spiralOrder = function (matrix) { - const m = matrix.length; - const n = matrix[0].length; - let x1 = 0; - let y1 = 0; - let x2 = m - 1; - let y2 = n - 1; - const ans = []; - while (x1 <= x2 && y1 <= y2) { - for (let j = y1; j <= y2; ++j) { - ans.push(matrix[x1][j]); - } - for (let i = x1 + 1; i <= x2; ++i) { - ans.push(matrix[i][y2]); - } - if (x1 < x2 && y1 < y2) { - for (let j = y2 - 1; j >= y1; --j) { - ans.push(matrix[x2][j]); - } - for (let i = x2 - 1; i > x1; --i) { - ans.push(matrix[i][y1]); - } - } - ++x1; - ++y1; - --x2; - --y2; - } - return ans; -}; -``` - -#### C# - -```cs -public class Solution { - public IList SpiralOrder(int[][] matrix) { - int m = matrix.Length, n = matrix[0].Length; - int x1 = 0, y1 = 0, x2 = m - 1, y2 = n - 1; - IList ans = new List(); - while (x1 <= x2 && y1 <= y2) { - for (int j = y1; j <= y2; ++j) { - ans.Add(matrix[x1][j]); - } - for (int i = x1 + 1; i <= x2; ++i) { - ans.Add(matrix[i][y2]); - } - if (x1 < x2 && y1 < y2) { - for (int j = y2 - 1; j >= y1; --j) { - ans.Add(matrix[x2][j]); - } - for (int i = x2 - 1; i > x1; --i) { - ans.Add(matrix[i][y1]); - } + for (int a = 0; a < m; ++a) { + for (int b = 0; b < n; ++b) { + matrix[a][b] -= 300; } - ++x1; - ++y1; - --x2; - --y2; } return ans; } diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution.cs b/solution/0000-0099/0054.Spiral Matrix/Solution.cs index 55a881802a84d..3c6f05743830a 100644 --- a/solution/0000-0099/0054.Spiral Matrix/Solution.cs +++ b/solution/0000-0099/0054.Spiral Matrix/Solution.cs @@ -1,14 +1,15 @@ public class Solution { public IList SpiralOrder(int[][] matrix) { int m = matrix.Length, n = matrix[0].Length; - int[] dirs = new int[] {0, 1, 0, -1, 0}; + int[] dirs = { 0, 1, 0, -1, 0 }; + int i = 0, j = 0, k = 0; IList ans = new List(); - bool[,] visited = new bool[m, n]; - for (int h = m * n, i = 0, j = 0, k = 0; h > 0; --h) { + bool[,] vis = new bool[m, n]; + for (int h = m * n; h > 0; --h) { ans.Add(matrix[i][j]); - visited[i, j] = true; + vis[i, j] = true; int x = i + dirs[k], y = j + dirs[k + 1]; - if (x < 0 || x >= m || y < 0 || y >= n || visited[x, y]) { + if (x < 0 || x >= m || y < 0 || y >= n || vis[x, y]) { k = (k + 1) % 4; } i += dirs[k]; diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution.java b/solution/0000-0099/0054.Spiral Matrix/Solution.java index 59bbaed593557..52c3fa7069d4f 100644 --- a/solution/0000-0099/0054.Spiral Matrix/Solution.java +++ b/solution/0000-0099/0054.Spiral Matrix/Solution.java @@ -17,4 +17,4 @@ public List spiralOrder(int[][] matrix) { } return ans; } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution.js b/solution/0000-0099/0054.Spiral Matrix/Solution.js index 1b9e8a60b500e..9d8404d9328db 100644 --- a/solution/0000-0099/0054.Spiral Matrix/Solution.js +++ b/solution/0000-0099/0054.Spiral Matrix/Solution.js @@ -6,7 +6,7 @@ var spiralOrder = function (matrix) { const m = matrix.length; const n = matrix[0].length; const ans = []; - const vis = new Array(m).fill(0).map(() => new Array(n).fill(false)); + const vis = Array.from({ length: m }, () => Array(n).fill(false)); const dirs = [0, 1, 0, -1, 0]; for (let h = m * n, i = 0, j = 0, k = 0; h > 0; --h) { ans.push(matrix[i][j]); diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution.py b/solution/0000-0099/0054.Spiral Matrix/Solution.py index 0affd4005acf8..718f3307a37f1 100644 --- a/solution/0000-0099/0054.Spiral Matrix/Solution.py +++ b/solution/0000-0099/0054.Spiral Matrix/Solution.py @@ -2,15 +2,15 @@ class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: m, n = len(matrix), len(matrix[0]) dirs = (0, 1, 0, -1, 0) + vis = [[False] * n for _ in range(m)] i = j = k = 0 ans = [] - vis = set() for _ in range(m * n): ans.append(matrix[i][j]) - vis.add((i, j)) + vis[i][j] = True x, y = i + dirs[k], j + dirs[k + 1] - if not 0 <= x < m or not 0 <= y < n or (x, y) in vis: + if x < 0 or x >= m or y < 0 or y >= n or vis[x][y]: k = (k + 1) % 4 - i = i + dirs[k] - j = j + dirs[k + 1] + i += dirs[k] + j += dirs[k + 1] return ans diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution.rs b/solution/0000-0099/0054.Spiral Matrix/Solution.rs index 04eb1e7c3e013..884cd8ff492f0 100644 --- a/solution/0000-0099/0054.Spiral Matrix/Solution.rs +++ b/solution/0000-0099/0054.Spiral Matrix/Solution.rs @@ -1,35 +1,28 @@ impl Solution { pub fn spiral_order(matrix: Vec>) -> Vec { - let mut x1 = 0; - let mut y1 = 0; - let mut x2 = matrix.len() - 1; - let mut y2 = matrix[0].len() - 1; - let mut result = vec![]; + let m = matrix.len(); + let n = matrix[0].len(); + let mut dirs = vec![0, 1, 0, -1, 0]; + let mut vis = vec![vec![false; n]; m]; + let mut i = 0; + let mut j = 0; + let mut k = 0; + let mut ans = Vec::new(); - while x1 <= x2 && y1 <= y2 { - for j in y1..=y2 { - result.push(matrix[x1][j]); - } - for i in x1 + 1..=x2 { - result.push(matrix[i][y2]); - } - if x1 < x2 && y1 < y2 { - for j in (y1..y2).rev() { - result.push(matrix[x2][j]); - } - for i in (x1 + 1..x2).rev() { - result.push(matrix[i][y1]); - } - } - x1 += 1; - y1 += 1; - if x2 != 0 { - x2 -= 1; - } - if y2 != 0 { - y2 -= 1; + for _ in 0..(m * n) { + ans.push(matrix[i][j]); + vis[i][j] = true; + let x = i as i32 + dirs[k] as i32; + let y = j as i32 + dirs[k + 1] as i32; + + if x < 0 || x >= m as i32 || y < 0 || y >= n as i32 || vis[x as usize][y as usize] { + k = (k + 1) % 4; } + + i = (i as i32 + dirs[k] as i32) as usize; + j = (j as i32 + dirs[k + 1] as i32) as usize; } - return result; + + ans } } diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution.ts b/solution/0000-0099/0054.Spiral Matrix/Solution.ts index fb06cc8b2bf8f..1d4ceb8d41db9 100644 --- a/solution/0000-0099/0054.Spiral Matrix/Solution.ts +++ b/solution/0000-0099/0054.Spiral Matrix/Solution.ts @@ -2,7 +2,7 @@ function spiralOrder(matrix: number[][]): number[] { const m = matrix.length; const n = matrix[0].length; const ans: number[] = []; - const vis = new Array(m).fill(0).map(() => new Array(n).fill(false)); + const vis: boolean[][] = Array.from({ length: m }, () => Array(n).fill(false)); const dirs = [0, 1, 0, -1, 0]; for (let h = m * n, i = 0, j = 0, k = 0; h > 0; --h) { ans.push(matrix[i][j]); diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution2.cpp b/solution/0000-0099/0054.Spiral Matrix/Solution2.cpp index 290f98d3eecd0..1902345ad3e00 100644 --- a/solution/0000-0099/0054.Spiral Matrix/Solution2.cpp +++ b/solution/0000-0099/0054.Spiral Matrix/Solution2.cpp @@ -1,24 +1,25 @@ -class Solution { -public: - vector spiralOrder(vector>& matrix) { - int m = matrix.size(), n = matrix[0].size(); - int dirs[5] = {0, 1, 0, -1, 0}; - vector ans; - for (int h = m * n, i = 0, j = 0, k = 0; h; --h) { - ans.push_back(matrix[i][j]); - matrix[i][j] += 300; - int x = i + dirs[k], y = j + dirs[k + 1]; - if (x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] > 100) { - k = (k + 1) % 4; - } - i += dirs[k]; - j += dirs[k + 1]; - } - // for (int i = 0; i < m; ++i) { - // for (int j = 0; j < n; ++j) { - // matrix[i][j] -= 300; - // } - // } - return ans; - } -}; \ No newline at end of file +class Solution { +public: + vector spiralOrder(vector>& matrix) { + int m = matrix.size(), n = matrix[0].size(); + int dirs[5] = {0, 1, 0, -1, 0}; + int i = 0, j = 0, k = 0; + vector ans; + for (int h = m * n; h; --h) { + ans.push_back(matrix[i][j]); + matrix[i][j] += 300; + int x = i + dirs[k], y = j + dirs[k + 1]; + if (x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] > 100) { + k = (k + 1) % 4; + } + i += dirs[k]; + j += dirs[k + 1]; + } + for (i = 0; i < m; ++i) { + for (j = 0; j < n; ++j) { + matrix[i][j] -= 300; + } + } + return ans; + } +}; diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution2.cs b/solution/0000-0099/0054.Spiral Matrix/Solution2.cs index 1e25df3f57569..9eed4c321f651 100644 --- a/solution/0000-0099/0054.Spiral Matrix/Solution2.cs +++ b/solution/0000-0099/0054.Spiral Matrix/Solution2.cs @@ -1,23 +1,24 @@ -public class Solution { - public IList SpiralOrder(int[][] matrix) { - int m = matrix.Length, n = matrix[0].Length; - int[] dirs = new int[] {0, 1, 0, -1, 0}; - IList ans = new List(); - for (int h = m * n, i = 0, j = 0, k = 0; h > 0; --h) { - ans.Add(matrix[i][j]); - matrix[i][j] += 300; - int x = i + dirs[k], y = j + dirs[k + 1]; - if (x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] > 100) { - k = (k + 1) % 4; - } - i += dirs[k]; - j += dirs[k + 1]; - } - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - matrix[i][j] -= 300; - } - } - return ans; - } -} +public class Solution { + public IList SpiralOrder(int[][] matrix) { + int m = matrix.Length, n = matrix[0].Length; + int[] dirs = { 0, 1, 0, -1, 0 }; + int i = 0, j = 0, k = 0; + IList ans = new List(); + for (int h = m * n; h > 0; --h) { + ans.Add(matrix[i][j]); + matrix[i][j] += 300; + int x = i + dirs[k], y = j + dirs[k + 1]; + if (x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] > 100) { + k = (k + 1) % 4; + } + i += dirs[k]; + j += dirs[k + 1]; + } + for (int a = 0; a < m; ++a) { + for (int b = 0; b < n; ++b) { + matrix[a][b] -= 300; + } + } + return ans; + } +} diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution2.go b/solution/0000-0099/0054.Spiral Matrix/Solution2.go index 433b797679502..04f69e96dc141 100644 --- a/solution/0000-0099/0054.Spiral Matrix/Solution2.go +++ b/solution/0000-0099/0054.Spiral Matrix/Solution2.go @@ -1,19 +1,20 @@ -func spiralOrder(matrix [][]int) (ans []int) { - m, n := len(matrix), len(matrix[0]) - dirs := [5]int{0, 1, 0, -1, 0} - for h, i, j, k := m*n, 0, 0, 0; h > 0; h-- { - ans = append(ans, matrix[i][j]) - matrix[i][j] += 300 - x, y := i+dirs[k], j+dirs[k+1] - if x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] > 100 { - k = (k + 1) % 4 - } - i, j = i+dirs[k], j+dirs[k+1] - } - // for i, row := range matrix { - // for j := range row { - // matrix[i][j] -= 300 - // } - // } - return -} \ No newline at end of file +func spiralOrder(matrix [][]int) (ans []int) { + m, n := len(matrix), len(matrix[0]) + dirs := [5]int{0, 1, 0, -1, 0} + i, j, k := 0, 0, 0 + for h := m * n; h > 0; h-- { + ans = append(ans, matrix[i][j]) + matrix[i][j] += 300 + x, y := i+dirs[k], j+dirs[k+1] + if x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] > 100 { + k = (k + 1) % 4 + } + i, j = i+dirs[k], j+dirs[k+1] + } + for i = 0; i < m; i++ { + for j = 0; j < n; j++ { + matrix[i][j] -= 300 + } + } + return +} diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution2.java b/solution/0000-0099/0054.Spiral Matrix/Solution2.java index c5d00818b96d5..012452c8f380a 100644 --- a/solution/0000-0099/0054.Spiral Matrix/Solution2.java +++ b/solution/0000-0099/0054.Spiral Matrix/Solution2.java @@ -1,23 +1,24 @@ -class Solution { - public List spiralOrder(int[][] matrix) { - int m = matrix.length, n = matrix[0].length; - int[] dirs = {0, 1, 0, -1, 0}; - List ans = new ArrayList<>(); - for (int h = m * n, i = 0, j = 0, k = 0; h > 0; --h) { - ans.add(matrix[i][j]); - matrix[i][j] += 300; - int x = i + dirs[k], y = j + dirs[k + 1]; - if (x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] > 100) { - k = (k + 1) % 4; - } - i += dirs[k]; - j += dirs[k + 1]; - } - // for (int i = 0; i < m; ++i) { - // for (int j = 0; j < n; ++j) { - // matrix[i][j] -= 300; - // } - // } - return ans; - } -} \ No newline at end of file +class Solution { + public List spiralOrder(int[][] matrix) { + int m = matrix.length, n = matrix[0].length; + int[] dirs = {0, 1, 0, -1, 0}; + int i = 0, j = 0, k = 0; + List ans = new ArrayList<>(); + for (int h = m * n; h > 0; --h) { + ans.add(matrix[i][j]); + matrix[i][j] += 300; + int x = i + dirs[k], y = j + dirs[k + 1]; + if (x < 0 || x >= m || y < 0 || y >= n || matrix[i][j] > 100) { + k = (k + 1) % 4; + } + i += dirs[k]; + j += dirs[k + 1]; + } + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + matrix[i][j] -= 300; + } + } + return ans; + } +} diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution2.js b/solution/0000-0099/0054.Spiral Matrix/Solution2.js index 7971309a3e4ac..3e2b75f26fdad 100644 --- a/solution/0000-0099/0054.Spiral Matrix/Solution2.js +++ b/solution/0000-0099/0054.Spiral Matrix/Solution2.js @@ -18,10 +18,10 @@ var spiralOrder = function (matrix) { i += dirs[k]; j += dirs[k + 1]; } - // for (let i = 0; i < m; ++i) { - // for (let j = 0; j < n; ++j) { - // matrix[i][j] -= 300; - // } - // } + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; ++j) { + matrix[i][j] -= 300; + } + } return ans; }; diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution2.py b/solution/0000-0099/0054.Spiral Matrix/Solution2.py index e4546d84417d1..ef2c6d7426f28 100644 --- a/solution/0000-0099/0054.Spiral Matrix/Solution2.py +++ b/solution/0000-0099/0054.Spiral Matrix/Solution2.py @@ -1,18 +1,18 @@ -class Solution: - def spiralOrder(self, matrix: List[List[int]]) -> List[int]: - m, n = len(matrix), len(matrix[0]) - dirs = (0, 1, 0, -1, 0) - i = j = k = 0 - ans = [] - for _ in range(m * n): - ans.append(matrix[i][j]) - matrix[i][j] += 300 - x, y = i + dirs[k], j + dirs[k + 1] - if not 0 <= x < m or not 0 <= y < n or matrix[x][y] > 100: - k = (k + 1) % 4 - i = i + dirs[k] - j = j + dirs[k + 1] - # for i in range(m): - # for j in range(n): - # matrix[i][j] -= 300 - return ans +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + m, n = len(matrix), len(matrix[0]) + dirs = (0, 1, 0, -1, 0) + i = j = k = 0 + ans = [] + for _ in range(m * n): + ans.append(matrix[i][j]) + matrix[i][j] += 300 + x, y = i + dirs[k], j + dirs[k + 1] + if x < 0 or x >= m or y < 0 or y >= n or matrix[x][y] > 100: + k = (k + 1) % 4 + i += dirs[k] + j += dirs[k + 1] + for i in range(m): + for j in range(n): + matrix[i][j] -= 300 + return ans diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution2.rs b/solution/0000-0099/0054.Spiral Matrix/Solution2.rs new file mode 100644 index 0000000000000..1cd086b94cb6a --- /dev/null +++ b/solution/0000-0099/0054.Spiral Matrix/Solution2.rs @@ -0,0 +1,38 @@ +impl Solution { + pub fn spiral_order(mut matrix: Vec>) -> Vec { + let m = matrix.len(); + let n = matrix[0].len(); + let mut dirs = vec![0, 1, 0, -1, 0]; + let mut i = 0; + let mut j = 0; + let mut k = 0; + let mut ans = Vec::new(); + + for _ in 0..(m * n) { + ans.push(matrix[i][j]); + matrix[i][j] += 300; + let x = i as i32 + dirs[k] as i32; + let y = j as i32 + dirs[k + 1] as i32; + + if x < 0 + || x >= m as i32 + || y < 0 + || y >= n as i32 + || matrix[x as usize][y as usize] > 100 + { + k = (k + 1) % 4; + } + + i = (i as i32 + dirs[k] as i32) as usize; + j = (j as i32 + dirs[k + 1] as i32) as usize; + } + + for i in 0..m { + for j in 0..n { + matrix[i][j] -= 300; + } + } + + ans + } +} diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution2.ts b/solution/0000-0099/0054.Spiral Matrix/Solution2.ts index 1a4282624c6e2..e4db7f5b9a0a7 100644 --- a/solution/0000-0099/0054.Spiral Matrix/Solution2.ts +++ b/solution/0000-0099/0054.Spiral Matrix/Solution2.ts @@ -14,10 +14,10 @@ function spiralOrder(matrix: number[][]): number[] { i += dirs[k]; j += dirs[k + 1]; } - // for (let i = 0; i < m; ++i) { - // for (let j = 0; j < n; ++j) { - // matrix[i][j] -= 300; - // } - // } + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; ++j) { + matrix[i][j] -= 300; + } + } return ans; } diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution3.cpp b/solution/0000-0099/0054.Spiral Matrix/Solution3.cpp deleted file mode 100644 index 1aa34c818b26a..0000000000000 --- a/solution/0000-0099/0054.Spiral Matrix/Solution3.cpp +++ /dev/null @@ -1,27 +0,0 @@ -class Solution { -public: - vector spiralOrder(vector>& matrix) { - int m = matrix.size(), n = matrix[0].size(); - int x1 = 0, y1 = 0, x2 = m - 1, y2 = n - 1; - vector ans; - while (x1 <= x2 && y1 <= y2) { - for (int j = y1; j <= y2; ++j) { - ans.push_back(matrix[x1][j]); - } - for (int i = x1 + 1; i <= x2; ++i) { - ans.push_back(matrix[i][y2]); - } - if (x1 < x2 && y1 < y2) { - for (int j = y2 - 1; j >= y1; --j) { - ans.push_back(matrix[x2][j]); - } - for (int i = x2 - 1; i > x1; --i) { - ans.push_back(matrix[i][y1]); - } - } - ++x1, ++y1; - --x2, --y2; - } - return ans; - } -}; \ No newline at end of file diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution3.cs b/solution/0000-0099/0054.Spiral Matrix/Solution3.cs deleted file mode 100644 index 7c4e1f17292c6..0000000000000 --- a/solution/0000-0099/0054.Spiral Matrix/Solution3.cs +++ /dev/null @@ -1,28 +0,0 @@ -public class Solution { - public IList SpiralOrder(int[][] matrix) { - int m = matrix.Length, n = matrix[0].Length; - int x1 = 0, y1 = 0, x2 = m - 1, y2 = n - 1; - IList ans = new List(); - while (x1 <= x2 && y1 <= y2) { - for (int j = y1; j <= y2; ++j) { - ans.Add(matrix[x1][j]); - } - for (int i = x1 + 1; i <= x2; ++i) { - ans.Add(matrix[i][y2]); - } - if (x1 < x2 && y1 < y2) { - for (int j = y2 - 1; j >= y1; --j) { - ans.Add(matrix[x2][j]); - } - for (int i = x2 - 1; i > x1; --i) { - ans.Add(matrix[i][y1]); - } - } - ++x1; - ++y1; - --x2; - --y2; - } - return ans; - } -} diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution3.go b/solution/0000-0099/0054.Spiral Matrix/Solution3.go deleted file mode 100644 index 593da1ab1f358..0000000000000 --- a/solution/0000-0099/0054.Spiral Matrix/Solution3.go +++ /dev/null @@ -1,23 +0,0 @@ -func spiralOrder(matrix [][]int) (ans []int) { - m, n := len(matrix), len(matrix[0]) - x1, y1, x2, y2 := 0, 0, m-1, n-1 - for x1 <= x2 && y1 <= y2 { - for j := y1; j <= y2; j++ { - ans = append(ans, matrix[x1][j]) - } - for i := x1 + 1; i <= x2; i++ { - ans = append(ans, matrix[i][y2]) - } - if x1 < x2 && y1 < y2 { - for j := y2 - 1; j >= y1; j-- { - ans = append(ans, matrix[x2][j]) - } - for i := x2 - 1; i > x1; i-- { - ans = append(ans, matrix[i][y1]) - } - } - x1, y1 = x1+1, y1+1 - x2, y2 = x2-1, y2-1 - } - return -} \ No newline at end of file diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution3.java b/solution/0000-0099/0054.Spiral Matrix/Solution3.java deleted file mode 100644 index 8fb658b6cfe86..0000000000000 --- a/solution/0000-0099/0054.Spiral Matrix/Solution3.java +++ /dev/null @@ -1,28 +0,0 @@ -class Solution { - public List spiralOrder(int[][] matrix) { - int m = matrix.length, n = matrix[0].length; - int x1 = 0, y1 = 0, x2 = m - 1, y2 = n - 1; - List ans = new ArrayList<>(); - while (x1 <= x2 && y1 <= y2) { - for (int j = y1; j <= y2; ++j) { - ans.add(matrix[x1][j]); - } - for (int i = x1 + 1; i <= x2; ++i) { - ans.add(matrix[i][y2]); - } - if (x1 < x2 && y1 < y2) { - for (int j = y2 - 1; j >= y1; --j) { - ans.add(matrix[x2][j]); - } - for (int i = x2 - 1; i > x1; --i) { - ans.add(matrix[i][y1]); - } - } - ++x1; - ++y1; - --x2; - --y2; - } - return ans; - } -} \ No newline at end of file diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution3.js b/solution/0000-0099/0054.Spiral Matrix/Solution3.js deleted file mode 100644 index 19fe4eaa70580..0000000000000 --- a/solution/0000-0099/0054.Spiral Matrix/Solution3.js +++ /dev/null @@ -1,34 +0,0 @@ -/** - * @param {number[][]} matrix - * @return {number[]} - */ -var spiralOrder = function (matrix) { - const m = matrix.length; - const n = matrix[0].length; - let x1 = 0; - let y1 = 0; - let x2 = m - 1; - let y2 = n - 1; - const ans = []; - while (x1 <= x2 && y1 <= y2) { - for (let j = y1; j <= y2; ++j) { - ans.push(matrix[x1][j]); - } - for (let i = x1 + 1; i <= x2; ++i) { - ans.push(matrix[i][y2]); - } - if (x1 < x2 && y1 < y2) { - for (let j = y2 - 1; j >= y1; --j) { - ans.push(matrix[x2][j]); - } - for (let i = x2 - 1; i > x1; --i) { - ans.push(matrix[i][y1]); - } - } - ++x1; - ++y1; - --x2; - --y2; - } - return ans; -}; diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution3.py b/solution/0000-0099/0054.Spiral Matrix/Solution3.py deleted file mode 100644 index 51102ba2e4b22..0000000000000 --- a/solution/0000-0099/0054.Spiral Matrix/Solution3.py +++ /dev/null @@ -1,18 +0,0 @@ -class Solution: - def spiralOrder(self, matrix: List[List[int]]) -> List[int]: - m, n = len(matrix), len(matrix[0]) - x1, y1, x2, y2 = 0, 0, m - 1, n - 1 - ans = [] - while x1 <= x2 and y1 <= y2: - for j in range(y1, y2 + 1): - ans.append(matrix[x1][j]) - for i in range(x1 + 1, x2 + 1): - ans.append(matrix[i][y2]) - if x1 < x2 and y1 < y2: - for j in range(y2 - 1, y1 - 1, -1): - ans.append(matrix[x2][j]) - for i in range(x2 - 1, x1, -1): - ans.append(matrix[i][y1]) - x1, y1 = x1 + 1, y1 + 1 - x2, y2 = x2 - 1, y2 - 1 - return ans diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution3.ts b/solution/0000-0099/0054.Spiral Matrix/Solution3.ts deleted file mode 100644 index 8438c09a6f7af..0000000000000 --- a/solution/0000-0099/0054.Spiral Matrix/Solution3.ts +++ /dev/null @@ -1,30 +0,0 @@ -function spiralOrder(matrix: number[][]): number[] { - const m = matrix.length; - const n = matrix[0].length; - let x1 = 0; - let y1 = 0; - let x2 = m - 1; - let y2 = n - 1; - const ans: number[] = []; - while (x1 <= x2 && y1 <= y2) { - for (let j = y1; j <= y2; ++j) { - ans.push(matrix[x1][j]); - } - for (let i = x1 + 1; i <= x2; ++i) { - ans.push(matrix[i][y2]); - } - if (x1 < x2 && y1 < y2) { - for (let j = y2 - 1; j >= y1; --j) { - ans.push(matrix[x2][j]); - } - for (let i = x2 - 1; i > x1; --i) { - ans.push(matrix[i][y1]); - } - } - ++x1; - ++y1; - --x2; - --y2; - } - return ans; -} diff --git a/solution/0000-0099/0073.Set Matrix Zeroes/README.md b/solution/0000-0099/0073.Set Matrix Zeroes/README.md index 06ebcdd85d874..c7ccb672c3c34 100644 --- a/solution/0000-0099/0073.Set Matrix Zeroes/README.md +++ b/solution/0000-0099/0073.Set Matrix Zeroes/README.md @@ -66,13 +66,15 @@ tags: -### 方法一:数组标记 +### Solution 1: Array Marking -我们分别用数组 `rows` 和 `cols` 标记待清零的行和列。 +Let the number of rows and columns of the matrix be $m$ and $n$, respectively. We use an array $\textit{rows}$ of length $m$ and an array $\textit{cols}$ of length $n$ to record which rows and columns need to be set to zero. -然后再遍历一遍矩阵,将 `rows` 和 `cols` 中标记的行和列对应的元素清零。 +First, we traverse the matrix. When we find a zero element in the matrix, we set the corresponding row and column markers to $\text{true}$. That is, if $\textit{matrix}[i][j] = 0$, then $\textit{rows}[i] = \textit{cols}[j] = \text{true}$. -时间复杂度 $O(m\times n)$,空间复杂度 $O(m+n)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。 +Finally, we traverse the matrix again and use the markers in $\textit{rows}$ and $\textit{cols}$ to update the elements in the matrix. When we find that $\textit{rows}[i]$ or $\textit{cols}[j]$ is $\text{true}$, we set $\textit{matrix}[i][j]$ to zero. + +The time complexity is $O(m \times n)$, and the space complexity is $O(m + n)$. Here, $m$ and $n$ are the number of rows and columns of the matrix, respectively. @@ -82,15 +84,15 @@ tags: class Solution: def setZeroes(self, matrix: List[List[int]]) -> None: m, n = len(matrix), len(matrix[0]) - rows = [0] * m - cols = [0] * n - for i, row in enumerate(matrix): - for j, v in enumerate(row): - if v == 0: - rows[i] = cols[j] = 1 + row = [False] * m + col = [False] * n for i in range(m): for j in range(n): - if rows[i] or cols[j]: + if matrix[i][j] == 0: + row[i] = col[j] = True + for i in range(m): + for j in range(n): + if row[i] or col[j]: matrix[i][j] = 0 ``` @@ -100,19 +102,18 @@ class Solution: class Solution { public void setZeroes(int[][] matrix) { int m = matrix.length, n = matrix[0].length; - boolean[] rows = new boolean[m]; - boolean[] cols = new boolean[n]; + boolean[] row = new boolean[m]; + boolean[] col = new boolean[n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (matrix[i][j] == 0) { - rows[i] = true; - cols[j] = true; + row[i] = col[j] = true; } } } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { - if (rows[i] || cols[j]) { + if (row[i] || col[j]) { matrix[i][j] = 0; } } @@ -128,19 +129,18 @@ class Solution { public: void setZeroes(vector>& matrix) { int m = matrix.size(), n = matrix[0].size(); - vector rows(m); - vector cols(n); + vector row(m); + vector col(n); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { - if (!matrix[i][j]) { - rows[i] = 1; - cols[j] = 1; + if (matrix[i][j] == 0) { + row[i] = col[j] = true; } } } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { - if (rows[i] || cols[j]) { + if (row[i] || col[j]) { matrix[i][j] = 0; } } @@ -153,20 +153,19 @@ public: ```go func setZeroes(matrix [][]int) { - m, n := len(matrix), len(matrix[0]) - rows := make([]bool, m) - cols := make([]bool, n) - for i, row := range matrix { - for j, v := range row { - if v == 0 { - rows[i] = true - cols[j] = true + row := make([]bool, len(matrix)) + col := make([]bool, len(matrix[0])) + for i := range matrix { + for j, x := range matrix[i] { + if x == 0 { + row[i] = true + col[j] = true } } } - for i := 0; i < m; i++ { - for j := 0; j < n; j++ { - if rows[i] || cols[j] { + for i := range matrix { + for j := range matrix[i] { + if row[i] || col[j] { matrix[i][j] = 0 } } @@ -183,19 +182,18 @@ func setZeroes(matrix [][]int) { function setZeroes(matrix: number[][]): void { const m = matrix.length; const n = matrix[0].length; - const rows: boolean[] = new Array(m).fill(false); - const cols: boolean[] = new Array(n).fill(false); + const row: boolean[] = Array(m).fill(false); + const col: boolean[] = Array(n).fill(false); for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { if (matrix[i][j] === 0) { - rows[i] = true; - cols[j] = true; + row[i] = col[j] = true; } } } for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { - if (rows[i] || cols[j]) { + if (row[i] || col[j]) { matrix[i][j] = 0; } } @@ -213,19 +211,18 @@ function setZeroes(matrix: number[][]): void { var setZeroes = function (matrix) { const m = matrix.length; const n = matrix[0].length; - const rows = new Array(m).fill(false); - const cols = new Array(n).fill(false); + const row = Array(m).fill(false); + const col = Array(n).fill(false); for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { - if (matrix[i][j] == 0) { - rows[i] = true; - cols[j] = true; + if (matrix[i][j] === 0) { + row[i] = col[j] = true; } } } for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { - if (rows[i] || cols[j]) { + if (row[i] || col[j]) { matrix[i][j] = 0; } } @@ -239,18 +236,18 @@ var setZeroes = function (matrix) { public class Solution { public void SetZeroes(int[][] matrix) { int m = matrix.Length, n = matrix[0].Length; - bool[] rows = new bool[m], cols = new bool[n]; + bool[] row = new bool[m], col = new bool[n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (matrix[i][j] == 0) { - rows[i] = true; - cols[j] = true; + row[i] = true; + col[j] = true; } } } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { - if (rows[i] || cols[j]) { + if (row[i] || col[j]) { matrix[i][j] = 0; } } diff --git a/solution/0000-0099/0073.Set Matrix Zeroes/README_EN.md b/solution/0000-0099/0073.Set Matrix Zeroes/README_EN.md index 2585078f01d16..cbd304c3c1b2e 100644 --- a/solution/0000-0099/0073.Set Matrix Zeroes/README_EN.md +++ b/solution/0000-0099/0073.Set Matrix Zeroes/README_EN.md @@ -78,15 +78,15 @@ The time complexity is $O(m\times n)$, and the space complexity is $O(m+n)$. Whe class Solution: def setZeroes(self, matrix: List[List[int]]) -> None: m, n = len(matrix), len(matrix[0]) - rows = [0] * m - cols = [0] * n - for i, row in enumerate(matrix): - for j, v in enumerate(row): - if v == 0: - rows[i] = cols[j] = 1 + row = [False] * m + col = [False] * n for i in range(m): for j in range(n): - if rows[i] or cols[j]: + if matrix[i][j] == 0: + row[i] = col[j] = True + for i in range(m): + for j in range(n): + if row[i] or col[j]: matrix[i][j] = 0 ``` @@ -96,19 +96,18 @@ class Solution: class Solution { public void setZeroes(int[][] matrix) { int m = matrix.length, n = matrix[0].length; - boolean[] rows = new boolean[m]; - boolean[] cols = new boolean[n]; + boolean[] row = new boolean[m]; + boolean[] col = new boolean[n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (matrix[i][j] == 0) { - rows[i] = true; - cols[j] = true; + row[i] = col[j] = true; } } } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { - if (rows[i] || cols[j]) { + if (row[i] || col[j]) { matrix[i][j] = 0; } } @@ -124,19 +123,18 @@ class Solution { public: void setZeroes(vector>& matrix) { int m = matrix.size(), n = matrix[0].size(); - vector rows(m); - vector cols(n); + vector row(m); + vector col(n); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { - if (!matrix[i][j]) { - rows[i] = 1; - cols[j] = 1; + if (matrix[i][j] == 0) { + row[i] = col[j] = true; } } } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { - if (rows[i] || cols[j]) { + if (row[i] || col[j]) { matrix[i][j] = 0; } } @@ -149,20 +147,19 @@ public: ```go func setZeroes(matrix [][]int) { - m, n := len(matrix), len(matrix[0]) - rows := make([]bool, m) - cols := make([]bool, n) - for i, row := range matrix { - for j, v := range row { - if v == 0 { - rows[i] = true - cols[j] = true + row := make([]bool, len(matrix)) + col := make([]bool, len(matrix[0])) + for i := range matrix { + for j, x := range matrix[i] { + if x == 0 { + row[i] = true + col[j] = true } } } - for i := 0; i < m; i++ { - for j := 0; j < n; j++ { - if rows[i] || cols[j] { + for i := range matrix { + for j := range matrix[i] { + if row[i] || col[j] { matrix[i][j] = 0 } } @@ -179,19 +176,18 @@ func setZeroes(matrix [][]int) { function setZeroes(matrix: number[][]): void { const m = matrix.length; const n = matrix[0].length; - const rows: boolean[] = new Array(m).fill(false); - const cols: boolean[] = new Array(n).fill(false); + const row: boolean[] = Array(m).fill(false); + const col: boolean[] = Array(n).fill(false); for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { if (matrix[i][j] === 0) { - rows[i] = true; - cols[j] = true; + row[i] = col[j] = true; } } } for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { - if (rows[i] || cols[j]) { + if (row[i] || col[j]) { matrix[i][j] = 0; } } @@ -199,6 +195,34 @@ function setZeroes(matrix: number[][]): void { } ``` +#### Rust + +```rust +impl Solution { + pub fn set_zeroes(matrix: &mut Vec>) { + let m = matrix.len(); + let n = matrix[0].len(); + let mut row = vec![false; m]; + let mut col = vec![false; n]; + for i in 0..m { + for j in 0..n { + if matrix[i][j] == 0 { + row[i] = true; + col[j] = true; + } + } + } + for i in 0..m { + for j in 0..n { + if row[i] || col[j] { + matrix[i][j] = 0; + } + } + } + } +} +``` + #### JavaScript ```js @@ -209,19 +233,18 @@ function setZeroes(matrix: number[][]): void { var setZeroes = function (matrix) { const m = matrix.length; const n = matrix[0].length; - const rows = new Array(m).fill(false); - const cols = new Array(n).fill(false); + const row = Array(m).fill(false); + const col = Array(n).fill(false); for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { - if (matrix[i][j] == 0) { - rows[i] = true; - cols[j] = true; + if (matrix[i][j] === 0) { + row[i] = col[j] = true; } } } for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { - if (rows[i] || cols[j]) { + if (row[i] || col[j]) { matrix[i][j] = 0; } } @@ -235,18 +258,18 @@ var setZeroes = function (matrix) { public class Solution { public void SetZeroes(int[][] matrix) { int m = matrix.Length, n = matrix[0].Length; - bool[] rows = new bool[m], cols = new bool[n]; + bool[] row = new bool[m], col = new bool[n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (matrix[i][j] == 0) { - rows[i] = true; - cols[j] = true; + row[i] = true; + col[j] = true; } } } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { - if (rows[i] || cols[j]) { + if (row[i] || col[j]) { matrix[i][j] = 0; } } diff --git a/solution/0000-0099/0073.Set Matrix Zeroes/Solution.cpp b/solution/0000-0099/0073.Set Matrix Zeroes/Solution.cpp index 16cc4dec3010c..4ce141536e45e 100644 --- a/solution/0000-0099/0073.Set Matrix Zeroes/Solution.cpp +++ b/solution/0000-0099/0073.Set Matrix Zeroes/Solution.cpp @@ -2,22 +2,21 @@ class Solution { public: void setZeroes(vector>& matrix) { int m = matrix.size(), n = matrix[0].size(); - vector rows(m); - vector cols(n); + vector row(m); + vector col(n); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { - if (!matrix[i][j]) { - rows[i] = 1; - cols[j] = 1; + if (matrix[i][j] == 0) { + row[i] = col[j] = true; } } } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { - if (rows[i] || cols[j]) { + if (row[i] || col[j]) { matrix[i][j] = 0; } } } } -}; \ No newline at end of file +}; diff --git a/solution/0000-0099/0073.Set Matrix Zeroes/Solution.cs b/solution/0000-0099/0073.Set Matrix Zeroes/Solution.cs index 128351f61b70a..d6e37478a090e 100644 --- a/solution/0000-0099/0073.Set Matrix Zeroes/Solution.cs +++ b/solution/0000-0099/0073.Set Matrix Zeroes/Solution.cs @@ -1,18 +1,18 @@ public class Solution { public void SetZeroes(int[][] matrix) { int m = matrix.Length, n = matrix[0].Length; - bool[] rows = new bool[m], cols = new bool[n]; + bool[] row = new bool[m], col = new bool[n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (matrix[i][j] == 0) { - rows[i] = true; - cols[j] = true; + row[i] = true; + col[j] = true; } } } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { - if (rows[i] || cols[j]) { + if (row[i] || col[j]) { matrix[i][j] = 0; } } diff --git a/solution/0000-0099/0073.Set Matrix Zeroes/Solution.go b/solution/0000-0099/0073.Set Matrix Zeroes/Solution.go index 05f7e65c331fb..7f3fbd883b3f3 100644 --- a/solution/0000-0099/0073.Set Matrix Zeroes/Solution.go +++ b/solution/0000-0099/0073.Set Matrix Zeroes/Solution.go @@ -1,20 +1,19 @@ func setZeroes(matrix [][]int) { - m, n := len(matrix), len(matrix[0]) - rows := make([]bool, m) - cols := make([]bool, n) - for i, row := range matrix { - for j, v := range row { - if v == 0 { - rows[i] = true - cols[j] = true + row := make([]bool, len(matrix)) + col := make([]bool, len(matrix[0])) + for i := range matrix { + for j, x := range matrix[i] { + if x == 0 { + row[i] = true + col[j] = true } } } - for i := 0; i < m; i++ { - for j := 0; j < n; j++ { - if rows[i] || cols[j] { + for i := range matrix { + for j := range matrix[i] { + if row[i] || col[j] { matrix[i][j] = 0 } } } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0073.Set Matrix Zeroes/Solution.java b/solution/0000-0099/0073.Set Matrix Zeroes/Solution.java index 0735d0e50a0a0..0f514f800695c 100644 --- a/solution/0000-0099/0073.Set Matrix Zeroes/Solution.java +++ b/solution/0000-0099/0073.Set Matrix Zeroes/Solution.java @@ -1,22 +1,21 @@ class Solution { public void setZeroes(int[][] matrix) { int m = matrix.length, n = matrix[0].length; - boolean[] rows = new boolean[m]; - boolean[] cols = new boolean[n]; + boolean[] row = new boolean[m]; + boolean[] col = new boolean[n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (matrix[i][j] == 0) { - rows[i] = true; - cols[j] = true; + row[i] = col[j] = true; } } } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { - if (rows[i] || cols[j]) { + if (row[i] || col[j]) { matrix[i][j] = 0; } } } } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0073.Set Matrix Zeroes/Solution.js b/solution/0000-0099/0073.Set Matrix Zeroes/Solution.js index 8a46036d11e59..bf4202ea136da 100644 --- a/solution/0000-0099/0073.Set Matrix Zeroes/Solution.js +++ b/solution/0000-0099/0073.Set Matrix Zeroes/Solution.js @@ -5,19 +5,18 @@ var setZeroes = function (matrix) { const m = matrix.length; const n = matrix[0].length; - const rows = new Array(m).fill(false); - const cols = new Array(n).fill(false); + const row = Array(m).fill(false); + const col = Array(n).fill(false); for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { - if (matrix[i][j] == 0) { - rows[i] = true; - cols[j] = true; + if (matrix[i][j] === 0) { + row[i] = col[j] = true; } } } for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { - if (rows[i] || cols[j]) { + if (row[i] || col[j]) { matrix[i][j] = 0; } } diff --git a/solution/0000-0099/0073.Set Matrix Zeroes/Solution.py b/solution/0000-0099/0073.Set Matrix Zeroes/Solution.py index d486145d0db8a..0ad6e62e84130 100644 --- a/solution/0000-0099/0073.Set Matrix Zeroes/Solution.py +++ b/solution/0000-0099/0073.Set Matrix Zeroes/Solution.py @@ -1,13 +1,13 @@ class Solution: def setZeroes(self, matrix: List[List[int]]) -> None: m, n = len(matrix), len(matrix[0]) - rows = [0] * m - cols = [0] * n - for i, row in enumerate(matrix): - for j, v in enumerate(row): - if v == 0: - rows[i] = cols[j] = 1 + row = [False] * m + col = [False] * n for i in range(m): for j in range(n): - if rows[i] or cols[j]: + if matrix[i][j] == 0: + row[i] = col[j] = True + for i in range(m): + for j in range(n): + if row[i] or col[j]: matrix[i][j] = 0 diff --git a/solution/0000-0099/0073.Set Matrix Zeroes/Solution.rs b/solution/0000-0099/0073.Set Matrix Zeroes/Solution.rs new file mode 100644 index 0000000000000..aa5d37572c427 --- /dev/null +++ b/solution/0000-0099/0073.Set Matrix Zeroes/Solution.rs @@ -0,0 +1,23 @@ +impl Solution { + pub fn set_zeroes(matrix: &mut Vec>) { + let m = matrix.len(); + let n = matrix[0].len(); + let mut row = vec![false; m]; + let mut col = vec![false; n]; + for i in 0..m { + for j in 0..n { + if matrix[i][j] == 0 { + row[i] = true; + col[j] = true; + } + } + } + for i in 0..m { + for j in 0..n { + if row[i] || col[j] { + matrix[i][j] = 0; + } + } + } + } +} diff --git a/solution/0000-0099/0073.Set Matrix Zeroes/Solution.ts b/solution/0000-0099/0073.Set Matrix Zeroes/Solution.ts index 7042732d5af20..72d77f6af4772 100644 --- a/solution/0000-0099/0073.Set Matrix Zeroes/Solution.ts +++ b/solution/0000-0099/0073.Set Matrix Zeroes/Solution.ts @@ -4,19 +4,18 @@ function setZeroes(matrix: number[][]): void { const m = matrix.length; const n = matrix[0].length; - const rows: boolean[] = new Array(m).fill(false); - const cols: boolean[] = new Array(n).fill(false); + const row: boolean[] = Array(m).fill(false); + const col: boolean[] = Array(n).fill(false); for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { if (matrix[i][j] === 0) { - rows[i] = true; - cols[j] = true; + row[i] = col[j] = true; } } } for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { - if (rows[i] || cols[j]) { + if (row[i] || col[j]) { matrix[i][j] = 0; } }