-
Notifications
You must be signed in to change notification settings - Fork 22
/
SpiralMatrix.java
117 lines (93 loc) · 3.23 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
package com.company.amazon;
import java.util.ArrayList;
import java.util.List;
public class SpiralMatrix {
public static void main(String[] args) {
int a[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
printSpiralMatrix(a);
System.out.println("\n================================");
System.out.println(spiralOrder(a));
System.out.println("\n================================");
a = new int[][]{{1, 2, 3, 4, 5, 6},
{7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18}
};
printSpiralMatrix(a);
System.out.println("\n================================");
System.out.println(spiralOrder(a));
}
public static void printSpiralMatrix(int[][] arr) {
int k = 0; // Start of Row
int l = 0; // Start of Column
int m = arr.length; // Total Rows
int n = arr[0].length; // Total Cols
int i = 0; // Simple Iterator
while (k < m && l < n) {
// Print 1st Row of the remaining rows
for (i = l; i <= n - 1; i++) {
System.out.print(arr[k][i] + ",");
}
k++; // First Row is traversed so increment the row
// Print Last Column of the remaining cols
for (i = k; i <= m - 1; i++) {
System.out.print(arr[i][n - 1] + ",");
}
n--; // Last Col is traversed the decrement the total cols
if (m > k) { // Only if some row left to be traversed
// Print the Last Row of the remaining rows
for (i = n - 1; i >= l; i--) {
System.out.print(arr[m - 1][i] + ",");
}
m--;
}
if (n > l) { // Only if some col left to be traversed
// Print the first column of the
for (i = m - 1; i >= k; i--) {
System.out.print(arr[i][l] + ",");
}
l++;
}
}
}
public static List<Integer> spiralOrder(int[][] matrix) {
int m = matrix.length; // Total Rows
int n = matrix[0].length; // Total Cols
List<Integer> result = new ArrayList<>();
int k = 0; // Start of Row
int l = 0; // Start of Col
int i = 0;
while (k < m && l < n) {
// Print top row
for (i = l; i < n; i++) {
result.add(matrix[k][i]);
}
k++; // Increment the row
// Print last Col
for (i = k; i < m; i++) {
result.add(matrix[i][n - 1]);
}
n--;
// If (Any Rows are present)
if (k < m) {
// Process the last Row
for (i = n - 1; i >= l; i--) {
result.add(matrix[m - 1][i]);
}
m--;
}
// If Any Col left to be processed
if (l < n) {
// Process first col
for (i = m - 1; i >= k; i--) {
result.add(matrix[i][l]);
}
l++;
}
}
return result;
}
}