|
1 | 1 | package g2301_2400.s2326_spiral_matrix_iv;
|
2 | 2 |
|
3 | 3 | // #Medium #Array #Linked_List #Matrix #Simulation
|
4 |
| -// #2022_07_03_Time_12_ms_(83.33%)_Space_239.8_MB_(16.67%) |
| 4 | +// #2022_07_15_Time_12_ms_(85.48%)_Space_61.9_MB_(90.83%) |
5 | 5 |
|
6 | 6 | import com_github_leetcode.ListNode;
|
7 |
| -import java.util.Arrays; |
8 | 7 |
|
9 | 8 | /*
|
10 | 9 | * Definition for singly-linked list.
|
|
16 | 15 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
17 | 16 | * }
|
18 | 17 | */
|
19 |
| -@SuppressWarnings({"java:S135", "java:S2583"}) |
20 | 18 | public class Solution {
|
| 19 | + private enum Direction { |
| 20 | + RIGHT, |
| 21 | + DOWN, |
| 22 | + LEFT, |
| 23 | + UP |
| 24 | + } |
| 25 | + |
21 | 26 | public int[][] spiralMatrix(int m, int n, ListNode head) {
|
22 |
| - int[][] result = new int[m][n]; |
23 |
| - for (int[] x : result) { |
24 |
| - Arrays.fill(x, -1); |
25 |
| - } |
26 |
| - int rowBegin = 0; |
27 |
| - int rowEnd = m - 1; |
28 |
| - int colBegin = 0; |
29 |
| - int colEnd = n - 1; |
30 |
| - while (rowBegin <= rowEnd && colBegin <= colEnd) { |
31 |
| - // traverse right |
32 |
| - for (int j = colBegin; j <= colEnd; j++) { |
33 |
| - result[rowBegin][j] = head.val; |
| 27 | + int[][] arr = new int[m][n]; |
| 28 | + int i = 0; |
| 29 | + int j = -1; |
| 30 | + Direction direction = Direction.RIGHT; |
| 31 | + // Boundaries |
| 32 | + // ++ after Left to right Horizontal traversed |
| 33 | + int a = 0; |
| 34 | + // -- after Down to Up vertical traversed |
| 35 | + int b = n - 1; |
| 36 | + // -- after Right to Left horizontal teversed |
| 37 | + int c = m - 1; |
| 38 | + // ++ after Down to Up vertical traversed |
| 39 | + int d = 0; |
| 40 | + for (int k = 0; k < m * n; ++k) { |
| 41 | + int val = -1; |
| 42 | + if (head != null) { |
| 43 | + val = head.val; |
34 | 44 | head = head.next;
|
35 |
| - if (head == null) { |
36 |
| - break; |
37 |
| - } |
38 | 45 | }
|
39 |
| - rowBegin++; |
40 |
| - if (head == null) { |
41 |
| - break; |
42 |
| - } |
43 |
| - // Traverse Down |
44 |
| - for (int j = rowBegin; j <= rowEnd; j++) { |
45 |
| - result[j][colEnd] = head.val; |
46 |
| - head = head.next; |
47 |
| - if (head == null) { |
| 46 | + switch (direction) { |
| 47 | + case RIGHT: |
| 48 | + ++j; |
| 49 | + if (j == b) { |
| 50 | + direction = Direction.DOWN; |
| 51 | + ++a; |
| 52 | + } |
48 | 53 | break;
|
49 |
| - } |
50 |
| - } |
51 |
| - colEnd--; |
52 |
| - if (head == null) { |
53 |
| - break; |
54 |
| - } |
55 |
| - if (rowBegin <= rowEnd) { |
56 |
| - // Traverse Left |
57 |
| - for (int j = colEnd; j >= colBegin; j--) { |
58 |
| - result[rowEnd][j] = head.val; |
59 |
| - head = head.next; |
60 |
| - if (head == null) { |
61 |
| - break; |
| 54 | + case DOWN: |
| 55 | + ++i; |
| 56 | + if (i == c) { |
| 57 | + direction = Direction.LEFT; |
62 | 58 | }
|
63 |
| - } |
64 |
| - if (head == null) { |
65 | 59 | break;
|
66 |
| - } |
67 |
| - } |
68 |
| - rowEnd--; |
69 |
| - if (head == null) { |
70 |
| - break; |
71 |
| - } |
72 |
| - if (colBegin <= colEnd) { |
73 |
| - // Traver Up |
74 |
| - for (int j = rowEnd; j >= rowBegin; j--) { |
75 |
| - result[j][colBegin] = head.val; |
76 |
| - head = head.next; |
77 |
| - if (head == null) { |
78 |
| - break; |
| 60 | + case LEFT: |
| 61 | + --j; |
| 62 | + if (j == d) { |
| 63 | + --c; |
| 64 | + direction = Direction.UP; |
| 65 | + } |
| 66 | + break; |
| 67 | + case UP: |
| 68 | + default: |
| 69 | + --i; |
| 70 | + if (i == a) { |
| 71 | + --b; |
| 72 | + ++d; |
| 73 | + direction = Direction.RIGHT; |
79 | 74 | }
|
80 |
| - } |
81 |
| - if (head == null) { |
82 | 75 | break;
|
83 |
| - } |
84 |
| - } |
85 |
| - colBegin++; |
86 |
| - if (head == null) { |
87 |
| - break; |
88 | 76 | }
|
| 77 | + arr[i][j] = val; |
89 | 78 | }
|
90 |
| - return result; |
| 79 | + return arr; |
91 | 80 | }
|
92 | 81 | }
|
0 commit comments