Skip to content

Commit ddfe102

Browse files
authored
[20251108] PGM / LV2 / 삼각 달팽이 / 강신지
1 parent 9f53554 commit ddfe102

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
```java
2+
import java.util.*;
3+
4+
class Solution {
5+
public int[] solution(int n) {
6+
int total = n * (n + 1) / 2;
7+
int[][] tri = new int[n][n];
8+
9+
int num = 1;
10+
int r = -1;
11+
int c = 0;
12+
int len = n;
13+
14+
while (len > 0) {
15+
for (int i = 0; i < len; i++) {
16+
tri[++r][c] = num++;
17+
}
18+
len--;
19+
if (len == 0) break;
20+
21+
for (int i = 0; i < len; i++) {
22+
tri[r][++c] = num++;
23+
}
24+
len--;
25+
if (len == 0) break;
26+
27+
for (int i = 0; i < len; i++) {
28+
tri[--r][--c] = num++;
29+
}
30+
len--;
31+
}
32+
33+
int[] answer = new int[total];
34+
int idx = 0;
35+
for (int i = 0; i < n; i++) {
36+
for (int j = 0; j <= i; j++) {
37+
answer[idx++] = tri[i][j];
38+
}
39+
}
40+
return answer;
41+
}
42+
}
43+
44+
```

0 commit comments

Comments
 (0)