Spiral Matrix II
Given n = 3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]Basic Idea:
class Solution { public int[][] generateMatrix(int n) { int[][] matrix = new int[n][n]; helper(matrix, 0, n, 1); return matrix; } // 用来做递归,offset为向中间偏移的层数, width 是当前层的宽和高 // count 为当前层第一个需要填的数字(左上角) private void helper(int[][] matrix, int offset, int width, int count) { if (width == 0) return; if (width == 1) { matrix[offset][offset] = count; return; } // 按照上右下左的顺序填入数,从 count 开始 // 这里到width-1是一个技巧,例如上面的矩阵,填入的顺序为 1,2; 3,4; 5,6; 7,8 for (int i = 0; i < width - 1; ++i) matrix[offset][i + offset] = count++; for (int i = 0; i < width - 1; ++i) matrix[i + offset][matrix.length - offset - 1] = count++; for (int i = 0; i < width - 1; ++i) matrix[matrix.length - offset - 1][matrix.length - offset - 1 - i] = count++; for (int i = 0; i < width - 1; ++i) matrix[matrix.length - offset - 1 - i][offset] = count++; helper(matrix, offset + 1, width - 2, count); } }
public class Solution { public int[][] generateMatrix(int n) { int[][] matrix = new int[n][n]; int offset = 0, size = n, count = 1; while (size > 1) { // top row, left to right for (int i = offset; i < offset + size - 1; ++i) { matrix[offset][i] = count++; } // right column, up to bottom for (int i = offset; i < offset + size - 1; ++i) { matrix[i][offset + size - 1] = count++; } // bottom row, right to left for (int i = offset + size - 1; i > offset; --i) { matrix[offset + size - 1][i] = count++; } // left column, bottom up for (int i = offset + size - 1; i > offset; --i) { matrix[i][offset] = count++; } // increase offset by 1, decrease size by 2 offset += 1; size -= 2; } // if the side length is odd, read the middle element manually if (size != 0) { matrix[n / 2][n / 2] = count++; } return matrix; } }
Update: Follow Up, what if input is <M, N> represent for width and height?
<M, N> represent for width and height?