Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ad300ff

Browse files
committed
solve spiral_matrix
1 parent 1ba26d0 commit ad300ff

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

‎algs/0054.spiral_matrix.go‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package algs
2+
3+
// SpiralOrder
4+
//
5+
// Given an m x n matrix, return all elements of the matrix in spiral order.
6+
//
7+
// Example 1:
8+
//
9+
// Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
10+
// Output: [1,2,3,6,9,8,7,4,5]
11+
//
12+
// Example 2:
13+
//
14+
// Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
15+
// Output: [1,2,3,4,8,12,11,10,9,5,6,7]
16+
//
17+
// Constraints:
18+
//
19+
// m == matrix.length
20+
// n == matrix[i].length
21+
// 1 <= m, n <= 10
22+
// -100 <= matrix[i][j] <= 100
23+
func SpiralOrder(matrix [][]int) []int {
24+
if len(matrix) == 0 {
25+
return nil
26+
}
27+
28+
m := len(matrix)
29+
n := len(matrix[0])
30+
res := make([]int, 0, m*n)
31+
32+
left, right, top, bottom := 0, n, 0, m
33+
for left < right && top < bottom && len(res) < cap(res) {
34+
// 向右
35+
for i := left; i < right && len(res) < cap(res); i++ {
36+
res = append(res, matrix[top][i])
37+
}
38+
top++
39+
// 向下
40+
for i := top; i < bottom && len(res) < cap(res); i++ {
41+
res = append(res, matrix[i][right-1])
42+
}
43+
right--
44+
// 向左
45+
for i := right - 1; i >= left && len(res) < cap(res); i-- {
46+
res = append(res, matrix[bottom-1][i])
47+
}
48+
bottom--
49+
// 向上
50+
for i := bottom - 1; i > top-1 && len(res) < cap(res); i-- {
51+
res = append(res, matrix[i][left])
52+
}
53+
left++
54+
}
55+
56+
return res
57+
}

‎src/solution.rs‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,4 +998,70 @@ impl Solution {
998998
}
999999
}
10001000
}
1001+
1002+
/// [54. Spiral Matrix](https://leetcode.cn/problems/spiral-matrix/description/)
1003+
///
1004+
/// Given an m x n matrix, return all elements of the matrix in spiral order.
1005+
///
1006+
/// Example 1:
1007+
///
1008+
/// Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
1009+
/// Output: [1,2,3,6,9,8,7,4,5]
1010+
///
1011+
/// Example 2:
1012+
///
1013+
/// Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
1014+
/// Output: [1,2,3,4,8,12,11,10,9,5,6,7]
1015+
///
1016+
/// Constraints:
1017+
///
1018+
/// m == matrix.length
1019+
/// n == matrix[i].length
1020+
/// 1 <= m, n <= 10
1021+
/// -100 <= matrix[i][j] <= 100
1022+
pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
1023+
if matrix.is_empty() {
1024+
return vec![];
1025+
}
1026+
let m = matrix.len();
1027+
let n = matrix[0].len();
1028+
let mut res = Vec::with_capacity(m * n);
1029+
1030+
let (mut left, mut right, mut top, mut bottom) = (0, n, 0, m);
1031+
while left < right && top < bottom && res.capacity() > res.len() {
1032+
// 向右
1033+
for i in left..right {
1034+
if res.capacity() <= res.len() {
1035+
break;
1036+
}
1037+
res.push(matrix[top][i]);
1038+
}
1039+
top += 1;
1040+
// 向下
1041+
for i in top..bottom {
1042+
if res.capacity() <= res.len() {
1043+
break;
1044+
}
1045+
res.push(matrix[i][right - 1]);
1046+
}
1047+
right -= 1;
1048+
// 向左
1049+
for i in (left..right).rev() {
1050+
if res.capacity() <= res.len() {
1051+
break;
1052+
}
1053+
res.push(matrix[bottom - 1][i]);
1054+
}
1055+
bottom -= 1;
1056+
// 向上
1057+
for i in (top..bottom).rev() {
1058+
if res.capacity() <= res.len() {
1059+
break;
1060+
}
1061+
res.push(matrix[i][left]);
1062+
}
1063+
left += 1;
1064+
}
1065+
res
1066+
}
10011067
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /