-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangleSnail2.java
More file actions
35 lines (30 loc) · 827 Bytes
/
Copy pathTriangleSnail2.java
File metadata and controls
35 lines (30 loc) · 827 Bytes
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
package basic;
class TriangleSnail2 {
public int[] solution(int n) {
int[] answer = new int[(n*(n+1))/2];
int[][] matrix = new int[n][n];
int x = -1, y = 0;
int num = 1;
for (int i = 0; i < n; ++i) {
for (int j = i; j < n; ++j) {
if (i % 3 == 0) {
++x;
} else if (i % 3 == 1) {
++y;
} else if (i % 3 == 2) {
--x;
--y;
}
matrix[x][y] = num++;
}
}
int k = 0;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
if(matrix[i][j] == 0) break;
answer[k++] = matrix[i][j];
}
}
return answer;
}
}