Skip to content

Commit 6b323d9

Browse files
Merge pull request #1636 from CodingTestStudy2/이진희
[이진희] Day05
2 parents d31156b + cde11cb commit 6b323d9

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
3+
1. 아이디어 : 같은 대각선은 r+c가 같다는 특징이 있으므로 대각선 번호 k를 0부터 m + n - 2까지 순회한다
4+
if문을 사용하여 어떤 방향으로 순회할지 결정한다
5+
k가 짝수이면 아래에서 위로, k가 홀수이면 위에서 아래로 이동한다
6+
7+
2. 시간복잡도 : O(M*N)
8+
9+
3. 자료구조/알고리즘 : 완전탐색
10+
11+
*/
12+
13+
class Solution {
14+
public int[] findDiagonalOrder(int[][] mat) {
15+
int m = mat.length;
16+
int n = mat[0].length;
17+
18+
int[] ans = new int[m*n];
19+
int idx = 0;
20+
21+
for (int k=0; k < m+n-1; k++) {
22+
if (k%2 == 0) {
23+
for (int r=m-1; r>=0; r--) {
24+
int c = k-r;
25+
if (c>=0 && c<n) {
26+
ans[idx++] = mat[r][c];
27+
}
28+
}
29+
} else {
30+
for (int r=0; r<m; r++) {
31+
int c = k-r;
32+
if (c>=0 && c<n) {
33+
ans[idx++] = mat[r][c];
34+
}
35+
}
36+
}
37+
}
38+
return ans;
39+
}
40+
}

0 commit comments

Comments
 (0)