-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTCI-Array-Matrix Rotation.cpp
More file actions
46 lines (41 loc) · 1.15 KB
/
CTCI-Array-Matrix Rotation.cpp
File metadata and controls
46 lines (41 loc) · 1.15 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
#include <bits/stdc++.h>
using namespace std;
void matrixTranspose(vector<vector<int>> &arr, int rowLen, int collLen){
for(int row = 0; row<rowLen; row++){
for(int coll = row + 1; coll<collLen; coll++){
int temp = arr[row][coll];
arr[row][coll] = arr[coll][row];
arr[coll][row] = temp;
}
}
}
void matrixRotation(vector<vector<int>> &arr,int rowLen, int collLen){
matrixTranspose(arr,rowLen,collLen);
for(int row = 0; row<rowLen; row++){
int begin =0, end = collLen-1;
while(begin<end){
int temp = arr[row][begin];
arr[row][begin] = arr[row][end];
arr[row][end] = temp;
begin++, end--;
}
}
}
int main() {
int rowLen, collLen;
cin>>rowLen>>collLen;
vector<vector<int>> arr(rowLen, vector<int> (collLen));
for(int row = 0; row<rowLen; row++){
for(int coll = 0; coll<collLen; coll++){
cin>>arr[row][coll];
}
}
matrixRotation(arr,rowLen, collLen);
for(int row = 0; row<rowLen; row++){
for(int coll = 0; coll<collLen; coll++){
cout<<arr[row][coll]<<" ";
}
cout<<endl;
}
return 0;
}