螺旋矩阵2
  螺旋矩阵2其实就是在1的基础上将遍历的数组值变为我们的值,思路和1一样。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
   | public int[][] generateMatrix(int n) {        int i = 1;        int left = 0;        int right = n;        int top = 0;        int bottom = n;        int row = 0;        int col = 0;        int [][] matrix = new int[n][n];        while(top<bottom||left<right){            for(col = left;col<right;col++){                matrix[top][col] = i;                i++;            }            for(row =top+1;row < bottom; row++){                matrix[row][right-1] = i;                i++;            }            if(top<bottom&&left<right){                for(col = right-2;col>=left;col--){                    matrix[bottom-1][col] = i;                    i++;                }                for(row = bottom-2; row > top; row-- ){                    matrix[row][left] = i;                    i++;                }            }            left++;            right--;            bottom--;            top++;        }        return matrix;    }
  |