题解 | #螺旋矩阵#C#
螺旋矩阵
http://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31
using System.Collections.Generic;
class Solution {
public List<int> spiralOrder (List<List<int>> matrix) {
// write code here
List<int> res = new List<int>();
if (matrix != null && matrix.Count == 0) return res;
int upBorder = 0,downBorder = matrix.Count-1,
leftBorder = 0,rightBorder = matrix[0].Count-1;
while (upBorder <= downBorder && leftBorder <= rightBorder)
{
for (int i = leftBorder; i <= rightBorder; i++)
res.Add(matrix[upBorder][i]);
upBorder++;
if (upBorder > downBorder) break;
for (int i = upBorder; i <= downBorder; i++)
res.Add(matrix[i][rightBorder]);
rightBorder--;
if (leftBorder > rightBorder) break;
for (int i = rightBorder; i >= leftBorder; i--)
res.Add(matrix[downBorder][i]);
downBorder--;
if (upBorder > downBorder) break;
for (int i = downBorder; i >= upBorder; i--)
res.Add(matrix[i][leftBorder]);
leftBorder++;
if (leftBorder > rightBorder) break;
}
return res;
}
}