-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathSpiralMatrixII.java
176 lines (130 loc) · 3.62 KB
/
SpiralMatrixII.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// @saorav21994
// TC : O (n^2)
// SC : O (n^2)
class Solution {
public int[][] generateMatrix(int n) {
int [][] ans = new int[n][n];
int m = 1;
int d = 0;
int dir[][] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int row = 0, col = 0;
while (m <= n * n) {
ans[row][col] = m++;
int r = Math.floorMod(row + dir[d][0], n);
int c = Math.floorMod(col + dir[d][1], n);
if (ans[r][c] != 0)
d = (d+1)%4;
row += dir[d][0];
col += dir[d][1];
}
return ans;
}
}
// TC : O(n2)
// SC : O(n2)
public class Solution {
public int[][] generateMatrix(int n) {
int[][] ans = new int[n][n];
if (n == 0) {
return ans;
}
int startRow = 0;
int endRow = n-1;
int startColumn = 0;
int endColumn = n - 1;
int count = 1;
while (startRow <= endRow && startColumn <= endColumn) {
// Move towards right
for (int j = startColumn; j <= endColumn; j ++) {
ans[startRow][j] = count++;
}
startRow++;
// Move towards down
for (int i = startRow; i <= endRow; i ++) {
ans[i][endColumn] = count++;
}
endColumn--;
if (startRow <= endRow) {
// Move towards left
for (int j = endColumn; j >= startColumn; j --) {
ans[endRow][j] = count++;
}
}
endRow--;
if (startColumn <= endColumn) {
// Move towards top
for (int i = endRow; i >= startRow; i --) {
ans[i][startColumn] = count++;
}
}
startColumn ++;
}
return ans;
}
}
// Author: @romitdutta10
// TC : O (n^2)
// SC : O (n^2)
// Problem : https://leetcode.com/problems/spiral-matrix-ii/
class Solution {
public int[][] generateMatrix(int n) {
int top = 0;
int bottom = n;
int right = n;
int left = 0;
int[][] arr = new int[n][n];
int num = 1;
while(top < bottom && left < right) {
for(int j=left; j< right; j++) {
arr[top][j] = num++;
}
top++;
for(int i=top; i<bottom; i++) {
arr[i][right-1] = num++;
}
right--;
for(int j=right-1; j>=left; j--) {
arr[bottom-1][j] = num++;
}
bottom--;
for(int i=bottom-1; i>=top; i--) {
arr[i][left] = num++;
}
left++;
}
return arr;
}
}
// Author: @romitdutta10
// TC : O (n^2)
// SC : O (n^2)
// Problem : https://leetcode.com/problems/spiral-matrix-ii/
class Solution {
public int[][] generateMatrix(int n) {
int top = 0;
int bottom = n;
int right = n;
int left = 0;
int[][] arr = new int[n][n];
int num = 1;
while(top < bottom && left < right) {
for(int j=left; j< right; j++) {
arr[top][j] = num++;
}
top++;
for(int i=top; i<bottom; i++) {
arr[i][right-1] = num++;
}
right--;
for(int j=right-1; j>=left; j--) {
arr[bottom-1][j] = num++;
}
bottom--;
for(int i=bottom-1; i>=top; i--) {
arr[i][left] = num++;
}
left++;
}
return arr;
}
}