-
Notifications
You must be signed in to change notification settings - Fork 199
/
GenerateMatrix1.java
executable file
·166 lines (132 loc) · 4.02 KB
/
GenerateMatrix1.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
package Algorithms.array;
public class GenerateMatrix1 {
public int[][] generateMatrix1(int n) {
int[][] ret = new int[n][n];
if (n == 0) {
// return a [] not a NULL.
return ret;
}
int number = 0;
int rows = n;
int x1 = 0;
int y1 = 0;
while (rows > 0) {
int x2 = x1 + rows - 1;
int y2 = y1 + rows - 1;
// the Whole first row.
for (int i = y1; i <= y2; i++) {
number++;
ret[x1][i] = number;
}
// the right column except the first and last line.
for (int i = x1 + 1; i < x2; i++) {
number++;
ret[i][y2] = number;
}
// This line is very important.
if (rows <= 1) {
break;
}
// the WHOLE last row.
for (int i = y2; i >= y1; i--) {
number++;
ret[x2][i] = number;
}
// the left column. column keep stable
// x: x2-1 --> x1 + 1
for (int i = x2 - 1; i > x1; i--) {
number++;
ret[i][y1] = number;
}
// remember this.
rows -= 2;
x1++;
y1++;
}
return ret;
}
/*
Solution 2: use direction.
*/
public int[][] generateMatrix2(int n) {
int[][] ret = new int[n][n];
if (n == 0) {
return ret;
}
int[] x = {1, 0, -1, 0};
int[] y = {0, 1, 0, -1};
int num = 0;
int step = 0;
int candElements = 0;
int visitedRows = 0;
int visitedCols = 0;
// 0: right, 1: down, 2: left, 3: up.
int direct = 0;
int startx = 0;
int starty = 0;
while (true) {
if (x[direct] == 0) {
// visit the Y axis
candElements = n - visitedRows;
} else {
// visit the X axis
candElements = n - visitedCols;
}
if (candElements <= 0) {
break;
}
// set the cell.
ret[startx][starty] = ++num;
step++;
// change the direction.
if (step == candElements) {
step = 0;
visitedRows += x[direct] == 0 ? 0: 1;
visitedCols += y[direct] == 0 ? 0: 1;
// change the direction.
direct = (direct + 1) % 4;
}
startx += y[direct];
starty += x[direct];
}
return ret;
}
/*
Solution 3: 使用四条bound来限制的方法.
*/
public int[][] generateMatrix(int n) {
int[][] ret = new int[n][n];
if (n == 0) {
return ret;
}
int top = 0, bottom = n - 1, left = 0, right = n - 1;
int num = 1;
while (top <= bottom) {
if (top == bottom) {
ret[top][top] = num++;
break;
}
// first line.
for (int i = left; i < right; i++) {
ret[top][i] = num++;
}
// right line;
for (int i = top; i < bottom; i++) {
ret[i][right] = num++;
}
// bottom line;
for (int i = right; i > left; i--) {
ret[bottom][i] = num++;
}
// left line;
for (int i = bottom; i > top; i--) {
ret[i][left] = num++;
}
top++;
bottom--;
left++;
right--;
}
return ret;
}
}