Skip to content

Commit 7e1c46a

Browse files
authored
Improved task 2326.
1 parent c7341b4 commit 7e1c46a

File tree

1 file changed

+52
-63
lines changed

1 file changed

+52
-63
lines changed
Lines changed: 52 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package g2301_2400.s2326_spiral_matrix_iv;
22

33
// #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%)
55

66
import com_github_leetcode.ListNode;
7-
import java.util.Arrays;
87

98
/*
109
* Definition for singly-linked list.
@@ -16,77 +15,67 @@
1615
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
1716
* }
1817
*/
19-
@SuppressWarnings({"java:S135", "java:S2583"})
2018
public class Solution {
19+
private enum Direction {
20+
RIGHT,
21+
DOWN,
22+
LEFT,
23+
UP
24+
}
25+
2126
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;
3444
head = head.next;
35-
if (head == null) {
36-
break;
37-
}
3845
}
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+
}
4853
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;
6258
}
63-
}
64-
if (head == null) {
6559
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;
7974
}
80-
}
81-
if (head == null) {
8275
break;
83-
}
84-
}
85-
colBegin++;
86-
if (head == null) {
87-
break;
8876
}
77+
arr[i][j] = val;
8978
}
90-
return result;
79+
return arr;
9180
}
9281
}

0 commit comments

Comments
 (0)