-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiralMatrix.java
More file actions
74 lines (61 loc) · 2.81 KB
/
SpiralMatrix.java
File metadata and controls
74 lines (61 loc) · 2.81 KB
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SpiralMatrix {
public static void main(String[] args) {
List<int[]> lst = new ArrayList<>();
lst.add(new int[]{ 1, 2, 3, 4 });
lst.add(new int[]{ 5, 6, 7, 8 });
lst.add(new int[]{ 9, 10, 11, 12 });
lst.add(new int[]{ 13, 14, 15, 16 });
System.out.println("[1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10]");
System.out.println(Arrays.toString(getSpiralMatrixGPT(lst)));
System.out.println(Arrays.toString(getSpiralMatrix(lst)));
}
public static int[] getSpiralMatrix(List<int[]> lst) {
int[] result = new int[lst.size() * lst.get(0).length];
int elLength = lst.get(0).length, idx = 0;
while (idx < result.length) {
// horizontal line = 1 2 3 4
for (int i = 0; i < elLength; i++) {
if (lst.get(0)[i] != -1) result[idx++] = lst.get(0)[i]; }
lst.remove(0); // control
// last indexes = 8 12
for (int i = 0; i < lst.size() - 1; i++) {
if (lst.get(i)[elLength - 1] != -1) {
result[elLength + i] = lst.get(i)[elLength - 1];
lst.get(i)[elLength - 1] = -1;} // control
idx++; }
// revert last list's element = 16 15 14 13
for (int i = elLength - 1; i >= 0; i--) {
if (lst.get(lst.size() - 1)[i] != -1) result[idx++] = lst.get(lst.size() - 1)[i]; }
lst.remove(lst.size() - 1); // control
// first indexes = 9 5
for (int i = 0; i < lst.size() - 1; i++) {
if (lst.get(lst.size() - 1)[i] != -1) {
result[idx++ + i] = lst.get(lst.size() - 1)[i];
lst.get(lst.size() - 1)[i] = -1;}} // control
}
return result;
}
public static int[] getSpiralMatrixGPT(List<int[]> lst) {
int[] result = new int[lst.size() * lst.get(0).length];
int top = 0, bottom = lst.size() - 1, left = 0, right = lst.get(0).length - 1;
int idx = 0;
while (left <= right && top <= bottom) {
for (int i = left; i <= right; i++) result[idx++] = lst.get(top)[i]; // top
top++;
for (int i = top; i <= bottom; i++) result[idx++] = lst.get(i)[right]; // right row
right--;
if (top <= bottom) {
for (int i = right; i >= left; i--) result[idx++] = lst.get(bottom)[i]; // bottom
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--) result[idx++] = lst.get(i)[left]; // left row
left++;
}
}
return result;
}
}