Skip to content

Commit 567085d

Browse files
feat:add typescript, rust, cpp solutions for lcof 29
1 parent 2d77da9 commit 567085d

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution
2+
{
3+
public:
4+
vector<int> spiralOrder(vector<vector<int>> &matrix)
5+
{
6+
vector<int> ans;
7+
if (matrix.size() == 0)
8+
return ans;
9+
int left = 0, top = 0, bottom = matrix.size() - 1, right = matrix[0].size() - 1;
10+
while (true)
11+
{
12+
for (int i = left; i <= right; i++)
13+
ans.push_back(matrix[top][i]);
14+
top++;
15+
if (top > bottom)
16+
break;
17+
for (int i = top; i <= bottom; i++)
18+
ans.push_back(matrix[i][right]);
19+
right--;
20+
if (right < left)
21+
break;
22+
for (int i = right; i >= left; i--)
23+
ans.push_back(matrix[bottom][i]);
24+
bottom--;
25+
if (bottom < top)
26+
break;
27+
for (int i = bottom; i >= top; i--)
28+
ans.push_back(matrix[i][left]);
29+
left++;
30+
if (left > right)
31+
break;
32+
}
33+
return ans;
34+
}
35+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
impl Solution {
2+
pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
3+
let mut ans=Vec::new();
4+
if matrix.len()==0{
5+
return ans;
6+
}
7+
let (mut left,mut right,mut top,mut bottom)=(0,matrix[0].len()-1,0,matrix.len()-1);
8+
loop{
9+
for i in left..right+1{
10+
ans.push(matrix[top][i]);
11+
}
12+
top+=1;
13+
if (top as i32)>(bottom as i32){
14+
break;
15+
}
16+
for i in top..bottom+1{
17+
ans.push(matrix[i][right]);
18+
}
19+
right-=1;
20+
if (right as i32)<(left as i32){
21+
break;
22+
}
23+
for i in (left..right+1).rev(){
24+
ans.push(matrix[bottom][i]);
25+
}
26+
bottom-=1;
27+
if (bottom as i32)<(top as i32){
28+
break;
29+
}
30+
for i in (top..bottom+1).rev(){
31+
ans.push(matrix[i][left]);
32+
}
33+
left+=1;
34+
if (left as i32)>(right as i32){
35+
break;
36+
}
37+
}
38+
ans
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var spiralOrder = (matrix: number[][]): number[] => {
2+
let ans: number[] = [];
3+
if (matrix.length === 0) return ans;
4+
let top = 0,
5+
left = 0,
6+
bottom = matrix.length - 1,
7+
right = matrix[0].length - 1;
8+
while (true) {
9+
for (let i = left; i <= right; i++) ans.push(matrix[top][i]);
10+
top++;
11+
if (top > bottom) break;
12+
for (let i = top; i <= bottom; i++) ans.push(matrix[i][right]);
13+
right--;
14+
if (right < left) break;
15+
for (let i = right; i >= left; i--) ans.push(matrix[bottom][i]);
16+
bottom--;
17+
if (bottom < top) break;
18+
for (let i = bottom; i >= top; i--) ans.push(matrix[i][left]);
19+
left++;
20+
if (left > right) break;
21+
}
22+
return ans;
23+
}

0 commit comments

Comments
 (0)