Reputation: 21
I thought of rotating a matrix 90 degrees, i also thought of turning rows to columns and columns to rows, i have thought of rotating the outer side first, the inner, and the middle stays the same since it's supposed to be 5x5. Anyways, i don't understand how to do it properly.
static int[][] multi = {
{ 3, 4, 5, 6, 7 },
{ 5, 4, 5, 6, 7 },
{ 6, 4, 5, 6, 7 },
{ 8, 4, 5, 6 ,7 },
{ 8, 4 ,5 ,6 ,7 }
};
public static void Rotate_90_Degrees() {
int temp = 0;
for(int i = 0; i < 5; i++) {
multi[i][0] = temp;
for(int j = 0; j < 5; j++) {
temp = multi[0][j];
}
}
}
i thought of going through the row, making a temporary variable, then when i get to the column, i will replace it with the temporary, the loop is supposed to continue. What do you say?
Upvotes: 2
Views: 189
Reputation: 521
I created an algortihm to rotate the matrix to the right by 90°. If you want a rotation to the left, you can simply rotate it 3 times to the right (if you don't care about performance of course :) ). This algorithm takes MxN matrices. If you only need to rotate NxN matrices, then you can do it inplace. For simplicity sake I didn't include that case in the algorithm.
I used String
as the matrix primitive type so that we can see the output cells better. Of course you can just make the same thing with int
as base type.
import java.util.Arrays;
public class YouSpinMyHeadRightRound
{
/**
* Rotates the matrix by 90 degrees. Input needs to
* be a "m x n" matrix.
*/
public static String[][] rotateRightBy90Degrees(String[][] inputMatrix)
{
int rows, columns;
rows = inputMatrix.length;
columns = inputMatrix[0].length;
int outputRows, outputColumns;
outputRows = columns;
outputColumns = rows;
String[][] output = new String[outputRows][outputColumns];
// fill the output matrix
for (int i = 0; i < outputColumns; i++)
{
for (int j = 0; j < outputRows; j++)
{
output[j][outputColumns - 1 - i] = inputMatrix[i][j];
}
}
return output;
}
/**
* Prints the matrix to console.
*/
public static void printMatrixToConsole(String[][] input)
{
for (int i = 0; i < input.length; i++)
{
System.out.println(Arrays.toString(input[i]));
}
}
/*
* For testing purposes!
*/
public static void main(String[] args)
{
String[][] matrixA = new String[][] {{"00", "01", "02", "03"},
{"10", "11", "12", "13"}, {"20", "21", "22", "23"}};
String[][] rotated90 = YouSpinMyHeadRightRound
.rotateRightBy90Degrees(matrixA);
String[][] rotated180 = YouSpinMyHeadRightRound
.rotateRightBy90Degrees(rotated90);
String[][] rotated270 = YouSpinMyHeadRightRound
.rotateRightBy90Degrees(rotated180);
String[][] rotated360 = YouSpinMyHeadRightRound
.rotateRightBy90Degrees(rotated270);
System.out.println("Initial matrix: ");
YouSpinMyHeadRightRound.printMatrixToConsole(matrixA);
System.out.println();
System.out.println("90° to the right:");
YouSpinMyHeadRightRound.printMatrixToConsole(rotated90);
System.out.println("180° to the right:");
YouSpinMyHeadRightRound.printMatrixToConsole(rotated180);
System.out.println("270° to the right:");
YouSpinMyHeadRightRound.printMatrixToConsole(rotated270);
System.out.println("360° to the right:");
YouSpinMyHeadRightRound.printMatrixToConsole(rotated360);
// the 360° matrix matches with matrixA
}
}
The output is:
Initial matrix:
[00, 01, 02, 03]
[10, 11, 12, 13]
[20, 21, 22, 23]
90° to the right:
[20, 10, 00]
[21, 11, 01]
[22, 12, 02]
[23, 13, 03]
180° to the right:
[23, 22, 21, 20]
[13, 12, 11, 10]
[03, 02, 01, 00]
270° to the right:
[03, 13, 23]
[02, 12, 22]
[01, 11, 21]
[00, 10, 20]
360° to the right:
[00, 01, 02, 03]
[10, 11, 12, 13]
[20, 21, 22, 23]
Upvotes: 0
Reputation:
Hint:
If you want to perform the rotation in-place, you will notice that the data movements are four-ways swaps like:
M[i,j] -> M[n+1-j,i] -> M[n+1-i,n+1-j] -> M[j,n+1-i] -> M[i,j]
Upvotes: 1