-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23_RotateMatrix.cpp
85 lines (75 loc) · 1.96 KB
/
23_RotateMatrix.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Given a square matrix of size N x N. The task is to rotate it by 90 degrees in anti-clockwise direction without using any extra space.
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
void PrintMatrix(vector<vector<int>> matrix, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
// Function to rotate matrix anticlockwise by 90 degrees.
void rotateby90(vector<vector<int>> &matrix, int n)
{
// Transpose:
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
// swap rows :
for (int i = 0; i < n / 2; i++)
{
for (int j = 0; j < n; j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[n - i - 1][j];
matrix[n - i - 1][j] = temp;
// swap(matrix[i][j],matrix[n-i-1][j]);
}
}
}
};
//{ Driver Code Starts.
int main()
{
int n;
cout<<"enter the row size:"<<endl;
cin >> n;
vector<vector<int>> matrix(n);
cout<<"enter the matrix elements:"<<endl;
for (int i = 0; i < n; i++)
{
matrix[i].assign(n, 0);
for (int j = 0; j < n; j++)
{
cin >> matrix[i][j];
}
}
Solution ob;
cout<<"original matrix:"<<endl;
ob.PrintMatrix(matrix,n,n);
cout<<"matrix after rotation of 90 degree anticlock-wise:"<<endl;
ob.rotateby90(matrix, n);
for (int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j){
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
// } Driver Code Ends