59.螺旋矩阵II class Solution { public int[][] generateMatrix(int n) { int[][] res = new int[n][n]; int count = 1; int left = 0,right = n-1,top = 0,bottom = n -1; while(left <= right && top <= bottom){ for(int col = left;col <= right;col++){ //从左往右 res[top][col] = count; count++; } top++; for(int row = top ;row <= bottom;row++){ res[row][right] = count; //从上到下 count++; } right--; //从右往左 for(int col = right ;col >= left;col--){ res[bottom][col] = count; count++; } bottom--; //从下往上 for(int row = bottom ;row >= top;row--){ res[row][left] = count; count++; } left++; } return res; } }