-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpiralMatrixIV.java
67 lines (60 loc) · 1.68 KB
/
SpiralMatrixIV.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.smlnskgmail.jaman.leetcodejava.medium;
import com.smlnskgmail.jaman.leetcodejava.support.ListNode;
import java.util.Arrays;
// https://leetcode.com/problems/spiral-matrix-iv/
public class SpiralMatrixIV {
private final int m;
private final int n;
private final ListNode head;
public SpiralMatrixIV(int m, int n, ListNode head) {
this.m = m;
this.n = n;
this.head = head;
}
public int[][] solution() {
int[][] result = new int[m][n];
for (int[] r : result) {
Arrays.fill(r, -1);
}
ListNode node = head;
int sR = 0;
int sC = 0;
int eR = m - 1;
int eC = n - 1;
while (node != null) {
for (int i = sC; i <= eC; i++) {
if (node == null) {
break;
}
result[sR][i] = node.val;
node = node.next;
}
for (int i = sR + 1; i <= eR; i++) {
if (node == null) {
break;
}
result[i][eC] = node.val;
node = node.next;
}
for (int i = eC - 1; i >= sC; i--) {
if (node == null) {
break;
}
result[eR][i] = node.val;
node = node.next;
}
for (int i = eR - 1; i > sR; i--) {
if (node == null) {
break;
}
result[i][sC] = node.val;
node = node.next;
}
sR++;
sC++;
eR--;
eC--;
}
return result;
}
}