-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreshapeMatrix.cpp
More file actions
50 lines (40 loc) · 1 KB
/
reshapeMatrix.cpp
File metadata and controls
50 lines (40 loc) · 1 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
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
int m = mat.size();
int n = mat[0].size();
if((m*n) != (r*c)){
return mat;
}
if(m == r && n == c){
return mat;
}
vector<vector<int>> newShape;
vector<int>row;
int tempC = 0;
for(int i = 0 ; i < m; i++){
for(int j = 0 ; j < n ; j++){
row.push_back(mat[i][j]);
tempC++;
if(tempC == c){
newShape.push_back(row);
row.clear();
tempC = 0;
}
}
}
return newShape;
}
int main(){
vector<vector<int>> test = {{1,2,3,4},{1,3,3,4},{2,5,3,6}};
int r = 3;
int c = 4;
vector<vector<int>> newShape = matrixReshape(test, r,c);
for(int i = 0 ; i < newShape.size() ; i++){
for(int j = 0 ; j < newShape[0].size() ; j++){
cout << newShape[i][j];
}
cout << endl;
}
return 0;
}