/
Solution.java
executable file
·73 lines (56 loc) · 2 KB
/
Solution.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
import java.util.LinkedList;
import java.util.List;
public class Solution {
public boolean check(int x, int y, int sizex, int sizey, boolean[][] vis){
return x >= 0 && x < sizex && y >= 0 && y < sizey && !vis[x][y];
}
public int[][] generateMatrix(int n) {
if(n == 0) return new int[][]{};
int[][] matrix = new int[n][n];
int[][] dir = {{0,1}, {1, 0}, {0, -1}, {-1, 0}};
int count = 0;
boolean[][] vis = new boolean[matrix.length][matrix[0].length];
int x = 0, y = 0;
for(int num = 1; num <= n*n; num++){
matrix[x][y] = num;
vis[x][y] = true;
int tmpx = x + dir[count][0];
int tmpy = y + dir[count][1];
if(tmpx < 0 || tmpx >= matrix.length || tmpy < 0 || tmpy >= matrix[0].length ||
vis[x + dir[count][0]][y + dir[count][1]]){
count = (count+1)%4;
}
x += dir[count][0];
y += dir[count][1];
}
return matrix;
}
}
public class Solution {
public int[][] generateMatrix(int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int[][] rt = new int[n][n];
if(n <= 0) return rt;
int c = 1;
int x = 0, y = 0, my = n , mx = n;
while(x < mx && y < my){
for(int i = x; i < mx && y < my; i++){
rt[y][i] = c++;
}
y++;
for(int i = y; i < my && x < mx ; i++ ){
rt[i][mx - 1] = c++;
}
mx--;
for(int i = mx - 1; i >= x && y < my; i--){
rt[my - 1][i] = c++;
}
my--;
for(int i = my - 1; i>= y && x < mx; i--){
rt[i][x] = c++;
}
x++;
}
return rt;
}
}