forked from dscmsit/Problem-Solving-in-any-Language
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransposeMatrix.cpp
More file actions
34 lines (34 loc) · 925 Bytes
/
TransposeMatrix.cpp
File metadata and controls
34 lines (34 loc) · 925 Bytes
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
class Solution {
public:
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
vector<vector<int>> ans;
int rows = matrix.size();
int cols = matrix[0].size();
if(rows < cols || rows > cols)
{
for(int i = 0; i < cols; i++)
{
vector<int> ans2;
for(int j = 0; j < rows; j++)
{
ans2.push_back(matrix[j][i]);
}
ans.push_back(ans2);
}
return ans;
}
else
{
for(int i = 0; i < rows; i++)
{
vector<int> ans2;
for(int j = 0; j < cols; j++)
{
ans2.push_back(matrix[j][i]);
}
ans.push_back(ans2);
}
return ans;
}
}
};