leetcode--普通题
题目1 . 输出螺旋矩阵
https://leetcode.com/problems/spiral-matrix/
题目描述:顺时针方向输出螺旋矩阵,逐步螺旋进中心, 将元素按此顺序输出
题解: 略
class Solution {
boolean[][] A= null ;
List<Integer> list = new ArrayList<>();
public List<Integer> spiralOrder(int[][] matrix) {
if(matrix.length == 0)return list;
A = new boolean[matrix.length][matrix[0].length];
//方向
int[] x_o = new int[] {0,1,0,-1};
int[] y_o = new int[] {1,0,-1,0};
//先把0加上
int x = 0;
int y = 0;
list.add(matrix[x][y]);
A[x][y] = true;
int count = 1;
int number = matrix.length*matrix[0].length;
while(count < number){
for(int i=0;i<4;i++){
while(canGo(x+x_o[i],y+y_o[i])){
x = x+x_o[i];
y = y+y_o[i];
list.add(matrix[x][y]);
A[x][y] = true;
count ++;
}
}
}
return list;
}
boolean canGo(int x,int y){
int m = A.length-1;
int n = A[0].length-1;
if(x >=0 && x <=m && y>=0 && y<= n &&A[x][y] == false){
return true;
}
return false;
}
}