File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+
4
+ public class Solution {
5
+ public IList < int > SpiralOrder ( int [ ] [ ] matrix ) {
6
+ List < int > result = new List < int > ( ) ;
7
+ int row = matrix . Length ;
8
+ if ( row == 0 ) return result ;
9
+ int column = matrix [ 0 ] . Length ;
10
+
11
+ int upRail = 0 ;
12
+ int downRail = row - 1 ;
13
+ int leftRail = 0 ;
14
+ int rightRail = column - 1 ;
15
+
16
+ while ( result . Count < row * column ) {
17
+ // L->R
18
+ for ( int c = leftRail ; c <= rightRail ; c ++ ) {
19
+ result . Add ( matrix [ upRail ] [ c ] ) ;
20
+ }
21
+ // T->B
22
+ for ( int r = upRail + 1 ; r <= downRail ; r ++ ) {
23
+ result . Add ( matrix [ r ] [ rightRail ] ) ;
24
+ }
25
+
26
+ // R->L
27
+ if ( upRail != downRail ) {
28
+ for ( int c = rightRail - 1 ; c >= leftRail ; c -- ) {
29
+ result . Add ( matrix [ downRail ] [ c ] ) ;
30
+ }
31
+ }
32
+
33
+ // B->T
34
+ if ( leftRail != rightRail ) {
35
+ for ( int r = downRail - 1 ; r > upRail ; r -- ) {
36
+ result . Add ( matrix [ r ] [ leftRail ] ) ;
37
+ }
38
+ }
39
+
40
+ leftRail += 1 ;
41
+ rightRail -= 1 ;
42
+ upRail += 1 ;
43
+ downRail -= 1 ;
44
+ }
45
+
46
+ return result ;
47
+ }
48
+ }
You can’t perform that action at this time.
0 commit comments