CC-18 :Rotate matrix by 90/180/270/360 degrees.
Description:
Given input int matrix mat, rotate its elements in place (without using an extra matrix) by 90/180/270/360 degrees and return the resultant matrix.
Test cases and expected outputs:
| Input | Expected outputs |
|---|---|
| rot=90 mat = { {7,1,4,1}, {9,9,3,0}, {2,1,4,3}, {6,2,7,9}} |
Rotated 90% : 6,2,9,7, 2,1,9,1, 7,4,3,4, 9,3,0,1, |
| Rot=180 mat = { {3,1,2,6,7}, {9,3,2,7,1}, {1,0,0,9,3}, {0,3,6,9,2}, {7,2,4,9,1}} |
Rotated 180% : 1,9,4,2,7, 2,9,6,3,0, 3,9,0,0,1, 1,7,2,3,9, 7,6,2,1,3, |
| Rot=270 mat = { {3,1,2,6,7}, {9,3,2,7,1}, {1,0,0,9,3}, {0,3,6,9,2}, {7,2,4,9,1}} |
Rotated 270% : 7,1,3,2,1, 6,7,9,9,9, 2,2,0,6,4, 1,3,0,3,2, 3,9,1,0,7, |
Pseudocode:
| The java method should accept following input parameters: mat (int square matrix), rot (int with values 90/180/270/360). |
| If rot = 360, there is nothing to be done as 360 degree rotation results in same matrix, so return mat. |
| Initialise variable n=mat.length. |
| To rotate the matrix in place we break down the matrix into squares. There are n/2 squares in a matrix of side n. We will rotate one square at a time using logic described below. |
| Declare in variables ogTop, ogRIght, ogBottom, ogLeft to hold references to 4 corners of square. |
| Iterate through mat using a for loop, using variable i as loop counter. Loop variable i will be initialized with value 0 and will iterate through all the matrix’s columns one by one till rIdx < mat.length/2:
Iterate through mat using a for loop, using variable j as loop counter. Loop variable j will be initialized with value i and will iterate through all the matrix’s columns one by one till j < mat[0].length:
Initialize below corner variables based on current square :
ogTop=mat[i][j]; ogRight=mat[j][n-1-i];
ogBottom=mat[n-1-i][n-1-j]; ogLeft=mat[n-1-j][i];
Note the sequence of original square : ogTop, ogRight, ogBottom, OgLeft.
If rotation angle is 90, rotate above original sequence by 1 step and assign to the 4 corners:
mat[i][j]=ogLeft; mat[j][n-1-i]=ogTop;
mat[n-1-i][n-1-j]=ogRight; mat[n-1-j][i]=ogBottom;
If rotation is 180, rotate above original sequence by 2 steps and assign to the 4 corners:
mat[i][j]=ogBottom; mat[j][n-1-i]=ogLeft;
mat[n-1-i][n-1-j]=ogTop; mat[n-1-j][i]=ogRight;
If rotation is 180, rotate above original sequence by 3 steps and assign to the 4 corners:
mat[i][j]=ogRight; mat[j][n-1-i]=ogBottom;
mat[n-1-i][n-1-j]=ogLeft; mat[n-1-j][i]=ogTop;
|
| After above loops complete, mat will be rotated by rot degrees. Return mat. |
Code:
public int[][] matRotate(int[][] mat, int rot) throws Exception{
if (rot==360) {return mat;}
int n=mat.length;
int ogTop, ogRight, ogBottom, ogLeft;
for (int i=0; i < mat.length/2; i++) {
for (int j=i; j< n-1-i; j++) {
ogTop=mat[i][j];
ogRight=mat[j][n-1-i];
ogBottom=mat[n-1-i][n-1-j];
ogLeft=mat[n-1-j][i];
if (rot==90) {
mat[i][j]=ogLeft; mat[j][n-1-i]=ogTop;
mat[n-1-i][n-1-j]=ogRight; mat[n-1-j][i]=ogBottom;
} else if (rot==180) {
mat[i][j]=ogBottom; mat[j][n-1-i]=ogLeft;
mat[n-1-i][n-1-j]=ogTop; mat[n-1-j][i]=ogRight;
}else if (rot==270) {
mat[i][j]=ogRight; mat[j][n-1-i]=ogBottom;
mat[n-1-i][n-1-j]=ogLeft; mat[n-1-j][i]=ogTop;
}
}
}
return mat;
}
Click here to download and run code and test cases !
| About Us | Privacy Policy | Contact us |