-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
885.java
140 lines (118 loc) · 3.59 KB
/
885.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
int[][] res;
int R;
int C;
public int[][] spiralMatrixIII(int R, int C, int r0, int c0) {
this.res = new int[R * C][];
this.R = R;
this.C = C;
fillArr(r0, c0, 0, 1);
return res;
}
private boolean isValid(int i, int j) {
return i >= 0 && i < R && j >= 0 && j < C;
}
private void fillArr(int row, int col, int index, int len) {
if (index == res.length) {
return;
}
if (len == 1) {
res[index] = new int[]{row, col};
index++;
len += 2;
fillArr(row, col + 1, index, len);
return;
}
int i = row;
int j = col;
int count = len - 1;
while (count > 0) {
if (isValid(i, j)) {
res[index] = new int[]{i, j};
index++;
}
i++;
count--;
}
i--;
j--;
count = len - 1;
while (count > 0) {
if (isValid(i, j)) {
res[index] = new int[]{i, j};
index++;
}
j--;
count--;
}
j++;
i--;
count = len - 1;
while (count > 0) {
if (isValid(i, j)) {
res[index] = new int[]{i, j};
index++;
}
i--;
count--;
}
i++;
j++;
count = len -1;
while (count > 0) {
if (isValid(i, j)) {
res[index] = new int[]{i, j};
index++;
}
j++;
count--;
}
len += 2;
fillArr(i, j, index, len);
}
}
__________________________________________________________________________________________________
sample 36420 kb submission
class Solution {
public int[][] spiralMatrixIII(int R, int C, int r0, int c0) {
int[] dirMoves = new int[4];
dirMoves[0] = 1; // right
dirMoves[1] = 1; // down
dirMoves[2] = 2; // left
dirMoves[3] = 2; // up
int[][] dirMap = new int[4][2];
dirMap[0] = new int[]{0, 1};
dirMap[1] = new int[]{1, 0};
dirMap[2] = new int[]{0, -1};
dirMap[3] = new int[]{-1, 0};
int maxVisit = R*C;
int visit = 0;
int[][] result = new int[maxVisit][2];
int currentDir = 0;
int currentR = r0;
int currentC = c0;
while(visit < maxVisit) {
int numMoves = 0;
while(numMoves < dirMoves[currentDir]) {
if(isValid(currentR, R - 1) && isValid(currentC, C - 1)) {
result[visit][0] = currentR;
result[visit][1] = currentC;
visit++;
}
currentR += dirMap[currentDir][0];
currentC += dirMap[currentDir][1];
numMoves++;
}
dirMoves[currentDir] += 2;
currentDir = (currentDir + 1) % 4;
}
return result;
}
private boolean isValid(int currentVal, int maxVal) {
if(currentVal < 0 || currentVal > maxVal) return false;
return true;
}
}
__________________________________________________________________________________________________