forked from phani653/Pattern-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzig_zag_array.cpp
35 lines (35 loc) · 1.01 KB
/
zig_zag_array.cpp
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
class Solution {
public:
vector<int> findDiagonalOrder(vector<vector<int>>& A) {
int n=A.size();
if(!n) return {};
int m=A[0].size();
int i=0, j=0, count=0, down=1;
vector<int> ans;
ans.push_back(A[i][j]); count++;
while(count<n*m){
if(down){
(j+1)<m ? j++ : i++;
ans.push_back(A[i][j]); count++;
if(count>=n*m) break;
while(i+1<n && j-1>=0){
i++; j--;
ans.push_back(A[i][j]); count++;
}
if(count>=n*m) break;
down=1-down;
}
if(!down){
(i+1)<n ? i++ : j++;
ans.push_back(A[i][j]); count++;
if(count>=n*m) break;
while(i-1>=0 && j+1<m){
i--; j++;
ans.push_back(A[i][j]); count++;
}
down=1-down;
}
}
return ans;
}
};