-
Notifications
You must be signed in to change notification settings - Fork 11
/
CS_55_PrintLikeAWave.cpp
41 lines (39 loc) · 957 Bytes
/
CS_55_PrintLikeAWave.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
36
37
38
39
40
41
#include <bits/stdc++.h>
using namespace std;
vector<int> wavePrint(vector<vector<int>> arr, int nRows, int mCols)
{
vector<int> ans;
for (int col = 0; col < mCols; col++)
{
if ((col & 1) == 0)
{
for (int row = 0; row < nRows; row++)
{
ans.push_back(arr[row][col]);
}
}
else
{
for (int row = nRows - 1; row >= 0; row--)
{
ans.push_back(arr[row][col]);
}
}
}
return ans;
}
int main()
{
vector<vector<int>> arr = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
int nRows = arr.size();
int mCols = arr[0].size();
vector<int> ans = wavePrint(arr, nRows, mCols);
for (int i = 0; i < ans.size(); i++)
{
cout << ans[i] << " ";
}
return 0;
}