LeetCode 59. Spiral Matrix II
2017-08-31 10:33
429 查看
class Solution {
public int[][] generateMatrix(int n) {
int[][] a = new int
;
int i = 0;
int j = 0;
int start = 0;
while (i < n && j < n) {
generate(i, j, a, n, start);
i++;
++j;
n--;
if (i < n && j < n)
start = a[i][j - 1];
}
return a;
}
public void generate(int row, int col, int[][] a, int n, int start) {
int i = row;
int j = col;
a[i][j] = start + 1;
for (j++; j < n; j++) a[i][j] = a[i][j - 1] + 1;
j--;
for (i++; i < n; i++) a[i][j] = a[i -1][j] + 1;
i--;
for (j--; j >= col && i != row; j--) a[i][j] = a[i][j + 1] + 1;
j++;
for (i--; i > row && j != n; i--) a[i][j] = a[i+1][j] + 1;
}
}
public int[][] generateMatrix(int n) {
int[][] a = new int
;
int i = 0;
int j = 0;
int start = 0;
while (i < n && j < n) {
generate(i, j, a, n, start);
i++;
++j;
n--;
if (i < n && j < n)
start = a[i][j - 1];
}
return a;
}
public void generate(int row, int col, int[][] a, int n, int start) {
int i = row;
int j = col;
a[i][j] = start + 1;
for (j++; j < n; j++) a[i][j] = a[i][j - 1] + 1;
j--;
for (i++; i < n; i++) a[i][j] = a[i -1][j] + 1;
i--;
for (j--; j >= col && i != row; j--) a[i][j] = a[i][j + 1] + 1;
j++;
for (i--; i > row && j != n; i--) a[i][j] = a[i+1][j] + 1;
}
}
相关文章推荐
- LeetCode 59. Spiral Matrix II
- LeetCode(59)SPiral Matrix II
- LeetCode59——Spiral Matrix II
- LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)
- <LeetCode OJ> 54 / 59 Spiral Matrix( I / II )
- leetcode-59 Spiral Matrix II
- 【leetcode】【59】Spiral Matrix II
- leetcode 59. Spiral Matrix II
- Leetcode 59. Spiral Matrix II
- leetcode 59. Spiral Matrix II
- LeetCode59 Spiral Matrix II
- [LeetCode 59] Spiral Matrix II
- LeetCode 54/59. Spiral Matrix i, ii
- LeetCode------54. Spiral Matrix(螺旋二维矩阵)-----59. Spiral Matrix II(n的螺旋矩阵)
- leetcode59 Spiral Matrix II
- [LeetCode] 59. Spiral Matrix II
- leetcode 59. Spiral Matrix II
- [leetcode-59]spiral matrixII(java)
- 59. Spiral Matrix II LeetCode
- 【LeetCode】C# 59、Spiral Matrix II