-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathProblem2.java
More file actions
52 lines (47 loc) · 1.49 KB
/
Problem2.java
File metadata and controls
52 lines (47 loc) · 1.49 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
46
47
48
49
50
51
52
// Time Complexity : O(M*N)
// Space Complexity : O(M*N)
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english
// Your code here along with comments explaining your approach
/*
Traverse the matrix diagonally, switching between upward and downward directions.
When moving up, bounce off the top row or last column; when moving down, bounce off the bottom row or first column.
Collect each cell in order while flipping the direction whenever a boundary is hit.
*/
public class Problem2 {
public int[] findDiagonalOrder(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
int[] res = new int[m*n];
int row = 0;
int col = 0;
boolean goingUp = true;
for(int i = 0;i<m*n;i++) {
res[i] = mat[row][col];
if(goingUp) {
if(col == n-1) {
goingUp = false;
row++;
} else if(row == 0) {
col++;
goingUp = false;
} else {
row--;
col++;
}
} else {
if(row == m-1) {
col++;
goingUp = true;
} else if(col == 0) {
row++;
goingUp = true;
} else {
col--;
row++;
}
}
}
return res;
}
}