File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments