Skip to content

Commit df4ee95

Browse files
Kevin Naughton JrKevin Naughton Jr
authored andcommitted
added SpiralMatrix.java
1 parent e815a7c commit df4ee95

File tree

4 files changed

+244
-0
lines changed

4 files changed

+244
-0
lines changed

company/google/SpiralMatrix.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
2+
//
3+
//Example 1:
4+
//
5+
//Input:
6+
//[
7+
//[ 1, 2, 3 ],
8+
//[ 4, 5, 6 ],
9+
//[ 7, 8, 9 ]
10+
//]
11+
//Output: [1,2,3,6,9,8,7,4,5]
12+
//Example 2:
13+
//
14+
//Input:
15+
//[
16+
//[1, 2, 3, 4],
17+
//[5, 6, 7, 8],
18+
//[9,10,11,12]
19+
//]
20+
//Output: [1,2,3,4,8,12,11,10,9,5,6,7]
21+
22+
class SpiralMatrix {
23+
public List<Integer> spiralOrder(int[][] matrix) {
24+
List<Integer> result = new ArrayList<Integer>();
25+
if(matrix == null || matrix.length == 0) {
26+
return result;
27+
}
28+
29+
int rowStart = 0;
30+
int rowEnd = matrix.length - 1;
31+
int colStart = 0;
32+
int colEnd = matrix[0].length - 1;
33+
while(rowStart <= rowEnd && colStart <= colEnd) {
34+
for(int i = colStart; i <= colEnd; i++) {
35+
result.add(matrix[rowStart][i]);
36+
}
37+
rowStart++;
38+
39+
for(int i = rowStart; i <= rowEnd; i++) {
40+
result.add(matrix[i][colEnd]);
41+
}
42+
colEnd--;
43+
44+
if(rowStart <= rowEnd) {
45+
for(int i = colEnd; i >= colStart; i--) {
46+
result.add(matrix[rowEnd][i]);
47+
}
48+
}
49+
rowEnd--;
50+
51+
if(colStart <= colEnd) {
52+
for(int i = rowEnd; i >= rowStart; i--) {
53+
result.add(matrix[i][colStart]);
54+
}
55+
}
56+
colStart++;
57+
}
58+
59+
return result;
60+
}
61+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
2+
//
3+
//Example 1:
4+
//
5+
//Input:
6+
//[
7+
//[ 1, 2, 3 ],
8+
//[ 4, 5, 6 ],
9+
//[ 7, 8, 9 ]
10+
//]
11+
//Output: [1,2,3,6,9,8,7,4,5]
12+
//Example 2:
13+
//
14+
//Input:
15+
//[
16+
//[1, 2, 3, 4],
17+
//[5, 6, 7, 8],
18+
//[9,10,11,12]
19+
//]
20+
//Output: [1,2,3,4,8,12,11,10,9,5,6,7]
21+
22+
class SpiralMatrix {
23+
public List<Integer> spiralOrder(int[][] matrix) {
24+
List<Integer> result = new ArrayList<Integer>();
25+
if(matrix == null || matrix.length == 0) {
26+
return result;
27+
}
28+
29+
int rowStart = 0;
30+
int rowEnd = matrix.length - 1;
31+
int colStart = 0;
32+
int colEnd = matrix[0].length - 1;
33+
while(rowStart <= rowEnd && colStart <= colEnd) {
34+
for(int i = colStart; i <= colEnd; i++) {
35+
result.add(matrix[rowStart][i]);
36+
}
37+
rowStart++;
38+
39+
for(int i = rowStart; i <= rowEnd; i++) {
40+
result.add(matrix[i][colEnd]);
41+
}
42+
colEnd--;
43+
44+
if(rowStart <= rowEnd) {
45+
for(int i = colEnd; i >= colStart; i--) {
46+
result.add(matrix[rowEnd][i]);
47+
}
48+
}
49+
rowEnd--;
50+
51+
if(colStart <= colEnd) {
52+
for(int i = rowEnd; i >= rowStart; i--) {
53+
result.add(matrix[i][colStart]);
54+
}
55+
}
56+
colStart++;
57+
}
58+
59+
return result;
60+
}
61+
}

company/uber/SpiralMatrix.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
2+
//
3+
//Example 1:
4+
//
5+
//Input:
6+
//[
7+
//[ 1, 2, 3 ],
8+
//[ 4, 5, 6 ],
9+
//[ 7, 8, 9 ]
10+
//]
11+
//Output: [1,2,3,6,9,8,7,4,5]
12+
//Example 2:
13+
//
14+
//Input:
15+
//[
16+
//[1, 2, 3, 4],
17+
//[5, 6, 7, 8],
18+
//[9,10,11,12]
19+
//]
20+
//Output: [1,2,3,4,8,12,11,10,9,5,6,7]
21+
22+
class SpiralMatrix {
23+
public List<Integer> spiralOrder(int[][] matrix) {
24+
List<Integer> result = new ArrayList<Integer>();
25+
if(matrix == null || matrix.length == 0) {
26+
return result;
27+
}
28+
29+
int rowStart = 0;
30+
int rowEnd = matrix.length - 1;
31+
int colStart = 0;
32+
int colEnd = matrix[0].length - 1;
33+
while(rowStart <= rowEnd && colStart <= colEnd) {
34+
for(int i = colStart; i <= colEnd; i++) {
35+
result.add(matrix[rowStart][i]);
36+
}
37+
rowStart++;
38+
39+
for(int i = rowStart; i <= rowEnd; i++) {
40+
result.add(matrix[i][colEnd]);
41+
}
42+
colEnd--;
43+
44+
if(rowStart <= rowEnd) {
45+
for(int i = colEnd; i >= colStart; i--) {
46+
result.add(matrix[rowEnd][i]);
47+
}
48+
}
49+
rowEnd--;
50+
51+
if(colStart <= colEnd) {
52+
for(int i = rowEnd; i >= rowStart; i--) {
53+
result.add(matrix[i][colStart]);
54+
}
55+
}
56+
colStart++;
57+
}
58+
59+
return result;
60+
}
61+
}

leetcode/array/SpiralMatrix.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
2+
//
3+
//Example 1:
4+
//
5+
//Input:
6+
//[
7+
//[ 1, 2, 3 ],
8+
//[ 4, 5, 6 ],
9+
//[ 7, 8, 9 ]
10+
//]
11+
//Output: [1,2,3,6,9,8,7,4,5]
12+
//Example 2:
13+
//
14+
//Input:
15+
//[
16+
//[1, 2, 3, 4],
17+
//[5, 6, 7, 8],
18+
//[9,10,11,12]
19+
//]
20+
//Output: [1,2,3,4,8,12,11,10,9,5,6,7]
21+
22+
class SpiralMatrix {
23+
public List<Integer> spiralOrder(int[][] matrix) {
24+
List<Integer> result = new ArrayList<Integer>();
25+
if(matrix == null || matrix.length == 0) {
26+
return result;
27+
}
28+
29+
int rowStart = 0;
30+
int rowEnd = matrix.length - 1;
31+
int colStart = 0;
32+
int colEnd = matrix[0].length - 1;
33+
while(rowStart <= rowEnd && colStart <= colEnd) {
34+
for(int i = colStart; i <= colEnd; i++) {
35+
result.add(matrix[rowStart][i]);
36+
}
37+
rowStart++;
38+
39+
for(int i = rowStart; i <= rowEnd; i++) {
40+
result.add(matrix[i][colEnd]);
41+
}
42+
colEnd--;
43+
44+
if(rowStart <= rowEnd) {
45+
for(int i = colEnd; i >= colStart; i--) {
46+
result.add(matrix[rowEnd][i]);
47+
}
48+
}
49+
rowEnd--;
50+
51+
if(colStart <= colEnd) {
52+
for(int i = rowEnd; i >= rowStart; i--) {
53+
result.add(matrix[i][colStart]);
54+
}
55+
}
56+
colStart++;
57+
}
58+
59+
return result;
60+
}
61+
}

0 commit comments

Comments
 (0)