-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSpiralMatrix.java
131 lines (115 loc) · 4.28 KB
/
SpiralMatrix.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
/*
* Given a matrix of m x n elements (m rows, n columns), return all
* elements of the matrix in spiral order.
*
Example 1:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]
Example 2:
Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
*/
import java.util.ArrayList;
import java.util.List;
public class SpiralMatrix {
public static void main (String[] args){
int[][] matrix = new int[3][3];
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[0][2] = 3;
matrix[1][0] = 4;
matrix[1][1] = 5;
matrix[1][2] = 6;
matrix[2][0] = 7;
matrix[2][1] = 8;
matrix[2][2] = 9;
List<Integer> arrayList = spiralOrder(matrix);
for (int item: arrayList) {
System.out.print(" " + item);
}
}
public static List<Integer> spiralOrder(int[][] matrix) {
// Initialize a new array list
List<Integer> arrayList = new ArrayList<>();
// If the matrix is empty, return it
if (matrix.length == 0) {
return arrayList;
}
// Sets the beginning of a row, default is index 0
int rowStart = 0;
// Sets the ending of a row, default is the final index of the row
int rowEnd = matrix.length-1;
// Sets the beginning of a column, default is index 0
int columnStart = 0;
// Sets the end of a column, default is the last index in the column
int columnEnd = matrix[0].length - 1;
/*
* While the row Start index is less than or equal to the row end index
* AND the column start index is less than or equal to the column end index
*
* First we go around the outer edges of the matrix
* */
while (rowStart <= rowEnd && columnStart <= columnEnd) {
/* Set the current index to the column start index and traverse right to the
* column end index*/
for (int currentIndex = columnStart; currentIndex <= columnEnd; currentIndex++) {
// Add each integer from the current row to the array list
arrayList.add(matrix[rowStart][currentIndex]);
}
// Move the beginning of the the row to the next index
rowStart++;
/*
* Set the current index to the row start index and traverse down to the row end
* index */
for (int currentIndex = rowStart; currentIndex <= rowEnd; currentIndex++) {
// Add each integer from the current column to the array list
arrayList.add(matrix[currentIndex][columnEnd]);
}
// Move the end of the column to the previous column index
columnEnd--;
/*
* Now we go around the inner parts of the matrix
* */
// If the row start index is less than or equal to the row end index
if (rowStart <= rowEnd) {
/*
* Set the current index to the column end index and traverse left to the column
* start index */
for (int currentIndex = columnEnd; currentIndex >= columnStart; currentIndex --) {
// Add each integer in the current row to the array list
arrayList.add(matrix[rowEnd][currentIndex]);
}
}
// Set the row end index to the previous row
rowEnd--;
// If the column start index is less than or equal to the column end index
if (columnStart <= columnEnd) {
/*
* Set the current index to the row end index and traverse up to the row start
* index */
for (int currentIndex = rowEnd; currentIndex >= rowStart; currentIndex --) {
// Add each integer from the current column to the array list
arrayList.add(matrix[currentIndex][columnStart]);
}
}
// Set the column start index to the next column
columnStart++;
}
// Return the array list
return arrayList;
}
}
/*
* Complexity Analysis:
* Time Complexity: O(N) where N is the number of integers
* Space Complexity: O(N) space where N is the arrayList */