Skip to content

Commit

Permalink
Create Rotate Image.cpp
Browse files Browse the repository at this point in the history
You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
  • Loading branch information
shrut012 authored Oct 31, 2022
1 parent 01965cb commit ee299c6
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Rotate Image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
*/


void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
int a = 0;
int b = n-1;
while(a<b){
for(int i=0;i<(b-a);++i){
swap(matrix[a][a+i], matrix[a+i][b]);
swap(matrix[a][a+i], matrix[b][b-i]);
swap(matrix[a][a+i], matrix[b-i][a]);
}
++a;
--b;
}
}

0 comments on commit ee299c6

Please sign in to comment.