-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSpiralMatrix3.java
80 lines (77 loc) · 3.01 KB
/
SpiralMatrix3.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
/*https://leetcode.com/problems/spiral-matrix-iii/*/
class Solution {
public int[][] spiralMatrixIII(int rows, int cols, int rStart, int cStart) {
int[][] result = new int[rows*cols][2];
int[][] a = new int[rows][cols];
int i = rStart, j = cStart, len = 2, index = 0, loop = 0;
//store the first index
result[index][0] = rStart;
result[index][1] = cStart;
++index;
boolean resultB = true;
//till we have more elements
while (index < rows*cols)
{
++j; //move right by one step
for (loop = 0; loop < len; ++loop) //keep moving for len length
{
if (i >= 0 && i < rows && j >= 0 && j < cols) //if the element exists
{
//store it
result[index][0] = i;
result[index][1] = j;
++index;
if (index == rows*cols){resultB = false; break;} //break if no more elements are present
}
++i; //move down
}
if (!resultB) break;
--i; //move up by one step
--j; //move left by one step
for (loop = 0; loop < len; ++loop) //keep moving for len length
{
if (i >= 0 && i < rows && j >= 0 && j < cols) //if the element exists
{
//store it
result[index][0] = i;
result[index][1] = j;
++index;
if (index == rows*cols){resultB = false; break;} //break if no more elements are present
}
--j; //move left
}
if (!resultB) break;
++j; //move right by one step
--i; //move up by one step
for (loop = 0; loop < len; ++loop) //keep moving for len length
{
if (i >= 0 && i < rows && j >= 0 && j < cols) //if the element exists
{
result[index][0] = i;
result[index][1] = j;
++index;
if (index == rows*cols){resultB = false; break;} //break if no more elements are present
}
--i; //move up
}
if (!resultB) break;
++i; //move down by one step
++j; //move right by one step
for (loop = 0; loop < len; ++loop) //keep moving for len length
{
if (i >= 0 && i < rows && j >= 0 && j < cols) //if the element exists
{
result[index][0] = i;
result[index][1] = j;
++index;
if (index == rows*cols){resultB = false; break;} //break if no more elements are present
}
++j; //move right
}
--j; //move left by one step
len += 2; //increase len by 2
if (!resultB) break;
}
return result;
}
}