59. Spiral Matrix II

time: O(mn)

space: O(mn)

class Solution {
    public int[][] generateMatrix(int n) {
        int res[][] = new int[n][n];
        
        int rowStart = 0;
        int rowEnd = n - 1;
        int colStart = 0;
        int colEnd = n - 1;
        int index = 1;
        
        while (rowStart <= rowEnd && colStart <= colEnd ) {
            for (int i = colStart; i <= colEnd; i++) {
                res[rowStart][i] = index++;
            }
            rowStart++;
            
            for (int i = rowStart; i<= rowEnd; i++) {
                res[i][colEnd] = index++;
            }
            colEnd--;
            
            if (rowStart <= rowEnd) {
                for (int i = colEnd; i >= colStart; i--) {
                    res[rowEnd][i] = index++;
                }
            }
            rowEnd--;
            
            if (colStart <= colEnd) {
                for (int i = rowEnd; i >= rowStart; i--) {
                    res[i][colStart] = index++;
                }
            }
            colStart++;
        }
        return res;
    }
}

Last updated