题解 | #螺旋矩阵#
螺旋矩阵
http://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31
public class Solution {
public ArrayList<Integer> spiralOrder(int[][] matrix) {
ArrayList<Integer> res = new ArrayList<>();
if(matrix.length == 0) return res;
int top = 0 , left = 0;
int bottom = matrix.length - 1;
int right = matrix[0].length - 1;
while(top <= bottom && left <= right){
for(int i = left; i <= right;i++){
res.add(matrix[top][i]);
}
top++;
if(top > bottom) break;
for(int i = top; i <= bottom;i++){
res.add(matrix[i][right]);
}
right--;
if(left > right) break;
for(int i = right; i >= left;i--){
res.add(matrix[bottom][i]);
}
bottom--;
for(int i = bottom;i >= top;i--){
res.add(matrix[i][left]);
}
left++;
}
return res;
}
}