-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrint matrix in diagonal pattern.cpp
More file actions
50 lines (47 loc) · 1.16 KB
/
Print matrix in diagonal pattern.cpp
File metadata and controls
50 lines (47 loc) · 1.16 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
vector<int> matrixDiagonally(vector<vector<int>>&mat)
{
int row=0 , col=0 , n=mat.size();
vector<int>ans;
bool up=true;
while(ans.size()!=n*n)
{
if(up)
{
while(row>0 && col<n-1)
{
ans.push_back(mat[row][col]);
row--;
col++;
}
ans.push_back(mat[row][col]);
if(col==n-1)
{
row++;
}
else
{
col++;
}
}
else
{
while(col>0 && row<n-1)
{
ans.push_back(mat[row][col]);
row++;
col--;
}
ans.push_back(mat[row][col]);
if(row==n-1)
{
col++;
}
else
{
row++;
}
}
up=!up;
}
return ans;
}