Skip to content

Commit cf44d0f

Browse files
authored
해설
1 parent e1c4fe0 commit cf44d0f

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

spiral-matrix/yeonguchoe.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

0 commit comments

Comments
 (0)