/
_0059_SpiralMatrixII.java
67 lines (63 loc) · 1.72 KB
/
_0059_SpiralMatrixII.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.diguage.algorithm.leetcode;
import static com.diguage.algorithm.util.PrintUtils.printMatrix;
/**
* = 59. Spiral Matrix II
*
* https://leetcode.com/problems/spiral-matrix-ii/[Spiral Matrix II - LeetCode]
*
* Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
*
* .Example:
* [source]
* ----
* Input: 3
* Output:
* [
* [ 1, 2, 3 ],
* [ 8, 9, 4 ],
* [ 7, 6, 5 ]
* ]
* ----
*
* @author D瓜哥, https://www.diguage.com/
* @since 2019-10-26 00:54
*/
public class _0059_SpiralMatrixII {
/**
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Spiral Matrix II.
*
* Memory Usage: 34.3 MB, less than 8.33% of Java online submissions for Spiral Matrix II.
*/
public int[][] generateMatrix(int n) {
int x1 = 0, x2 = n - 1;
int y1 = 0, y2 = n - 1;
int i = 1;
int[][] matrix = new int[n][n];
while (x1 <= x2 && y1 <= y2) {
for (int x = x1; x <= x2; x++) {
matrix[y1][x] = i++;
}
for (int y = y1 + 1; y <= y2; y++) {
matrix[y][x2] = i++;
}
if (x1 < x2 && y1 < y2) {
for (int x = x2 - 1; x > x1; x--) {
matrix[y2][x] = i++;
}
for (int y = y2; y > y1; y--) {
matrix[y][x1] = i++;
}
}
x1++;
x2--;
y1++;
y2--;
}
return matrix;
}
public static void main(String[] args) {
_0059_SpiralMatrixII solution = new _0059_SpiralMatrixII();
int[][] r1 = solution.generateMatrix(5);
printMatrix(r1);
}
}