-
Notifications
You must be signed in to change notification settings - Fork 2
/
shun-shi-zhen-da-yin-ju-zhen-lcof.rs
73 lines (63 loc) · 1.93 KB
/
shun-shi-zhen-da-yin-ju-zhen-lcof.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#![allow(dead_code, unused, unused_variables, non_snake_case)]
use serde::__private::de;
fn main() {}
struct Solution;
enum Direction {
Right,
Down,
Left,
Up,
}
impl Solution {
pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
if matrix.is_empty() {
return vec![];
}
let (mut left, mut right, mut up, mut down) = (0, matrix[0].len(), 0, matrix.len());
let mut data = Vec::with_capacity(right * down);
let (mut index1, mut index2) = (0, 0);
let mut direction = Direction::Right;
while left < right && up < down {
data.push(matrix[index1][index2]);
match direction {
Direction::Right => {
if index2 < right - 1 {
index2 += 1;
} else {
direction = Direction::Down;
index1 += 1;
up += 1;
}
}
Direction::Down => {
if index1 < down - 1 {
index1 += 1;
} else {
direction = Direction::Left;
index2 -= 1;
right -= 1;
}
}
Direction::Left => {
if index2 > left + 1 {
index2 -= 1;
} else {
direction = Direction::Up;
index1 -= 1;
down -= 1;
}
}
Direction::Up => {
if index1 > up + 1 {
index1 -= 1;
} else {
direction = Direction::Right;
index2 += 1;
left += 1;
}
}
}
}
data
}
}