Spiral-matrix-ii

HowieLee59發表於2018-06-27

題目描述


Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order.

For example,
Given n =3,

You should return the following matrix:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]
public class Solution {
public int[][] generateMatrix(int n) {
        int[][] res = new int[n][n];
        if (n < 1)
            return res;
        int index = 1, rowStart = 0, rowEnd = n - 1, colStart = 0, colEnd = n - 1;
        while (index <= n * n) {
            for (int i = colStart; i <= colEnd; i++) {
                res[rowStart][i] = index++;
            }
            for (int i = rowStart + 1; i <= rowEnd; i++) {
                res[i][colEnd] = index++;
            }
            for (int i = colEnd - 1; i >= colStart; i--) {
                res[rowEnd][i] = index++;
            }
            for (int i = rowEnd - 1; i > rowStart; i--) {
                res[i][colStart] = index++;
            }
            rowStart += 1;
            rowEnd -= 1;
            colStart += 1;
            colEnd -= 1;
        }
        return res;
    }
}