-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangleSnail3.java
More file actions
45 lines (37 loc) · 1.1 KB
/
Copy pathTriangleSnail3.java
File metadata and controls
45 lines (37 loc) · 1.1 KB
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
package basic;
public class TriangleSnail3 {
private static final int dy[] = {1, 0, -1};
private static final int dx[] = {0, 1, -1};
public static int[] solution(int n) {
int x = 0;
int y = 0;
int v = 1;
int nx = 0;
int ny = 0;
int index = 0;
int[][] triangle = new int[n][n];
while (true) {
triangle[y][x] = v++;
nx = x + dx[index];
ny = y + dy[index];
if (nx == n || ny == n || nx == -1 || ny == -1 || triangle[ny][nx] != 0) {
index = (index + 1) % 3;
nx = x + dx[index];
ny = y + dy[index];
if (nx == n || ny == n || nx == -1 || ny == -1 || triangle[ny][nx] != 0)
break;
}
x = nx;
y = ny;
}
int[] result = new int[n * (n + 1) / 2];
index = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
result[index] = triangle[i][j];
index++;
}
}
return result;
}
}