59. Spiral Matrix II
class Solution { public int[][] generateMatrix(int n) { int[][] matrix=new int[n][n]; generateMatrix(0, 0, 0, n-1, n-1, matrix); return matrix; } private void generateMatrix(int num, int top, int left, int bottom, int right, int[][] matrix){ if(top>bottom||left>right) return; for(int k=left;k<=right;k++) matrix[top][k]=++num; for(int k=top+1;k<=bottom;k++) matrix[k][right]=++num; if(top!=bottom) for(int k=right-1;k>=left;k--) matrix[bottom][k]=++num; if(left!=right) for(int k=bottom-1;k>top;k--) matrix[k][left]=++num; generateMatrix(num, top+1, left+1, bottom-1, right-1, matrix); } }