0059 Spiral Matrix II
Last updated
Last updated
My Solution:
class Solution {
public int[][] generateMatrix(int n) {
int[][] matrix = new int[n][n];
if(n == 0){
return matrix;
}
int rowStart = 0;
int rowEnd = n-1;
int colStart = 0;
int colENd = n-1;
int num = 1;
while(rowStart <= rowEnd
&& colStart <= colENd){
for(int i = colStart; i <= colENd; i++){
matrix[rowStart][i] = num++;
}
rowStart++;
for(int i = rowStart; i <= rowEnd; i++){
matrix[i][colENd] = num++;
}
colENd--;
for(int i = colENd; i >= colStart; i--){
if(rowStart <= rowEnd){
matrix[rowEnd][i] = num++;
}
}
rowEnd--;
for(int i = rowEnd; i>= rowStart;i--){
if(colStart <= colENd){
matrix[i][colStart] = num++;
}
}
colStart++;
}
return matrix;
}
}