Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0054 (#3897)
Browse files Browse the repository at this point in the history
No.0054.Spiral Matrix
  • Loading branch information
yanglbme authored Dec 28, 2024
1 parent 1a6ab2a commit 988caec
Show file tree
Hide file tree
Showing 33 changed files with 621 additions and 1,123 deletions.
444 changes: 125 additions & 319 deletions solution/0000-0099/0054.Spiral Matrix/README.md

Large diffs are not rendered by default.

444 changes: 125 additions & 319 deletions solution/0000-0099/0054.Spiral Matrix/README_EN.md

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions solution/0000-0099/0054.Spiral Matrix/Solution.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
public class Solution {
public IList<int> 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<int> ans = new List<int>();
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];
Expand Down
2 changes: 1 addition & 1 deletion solution/0000-0099/0054.Spiral Matrix/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ public List<Integer> spiralOrder(int[][] matrix) {
}
return ans;
}
}
}
2 changes: 1 addition & 1 deletion solution/0000-0099/0054.Spiral Matrix/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
10 changes: 5 additions & 5 deletions solution/0000-0099/0054.Spiral Matrix/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
49 changes: 21 additions & 28 deletions solution/0000-0099/0054.Spiral Matrix/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
impl Solution {
pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
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
}
}
2 changes: 1 addition & 1 deletion solution/0000-0099/0054.Spiral Matrix/Solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
49 changes: 25 additions & 24 deletions solution/0000-0099/0054.Spiral Matrix/Solution2.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
int dirs[5] = {0, 1, 0, -1, 0};
vector<int> 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;
}
};
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& 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<int> 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;
}
};
47 changes: 24 additions & 23 deletions solution/0000-0099/0054.Spiral Matrix/Solution2.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
public class Solution {
public IList<int> SpiralOrder(int[][] matrix) {
int m = matrix.Length, n = matrix[0].Length;
int[] dirs = new int[] {0, 1, 0, -1, 0};
IList<int> ans = new List<int>();
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<int> 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<int> ans = new List<int>();
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;
}
}
39 changes: 20 additions & 19 deletions solution/0000-0099/0054.Spiral Matrix/Solution2.go
Original file line number Diff line number Diff line change
@@ -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
}
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
}
47 changes: 24 additions & 23 deletions solution/0000-0099/0054.Spiral Matrix/Solution2.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
int[] dirs = {0, 1, 0, -1, 0};
List<Integer> 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;
}
}
class Solution {
public List<Integer> 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<Integer> 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;
}
}
10 changes: 5 additions & 5 deletions solution/0000-0099/0054.Spiral Matrix/Solution2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
36 changes: 18 additions & 18 deletions solution/0000-0099/0054.Spiral Matrix/Solution2.py
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 988caec

Please sign in to comment.