[英]Rotating Matrix 90 Degrees
我想過將矩陣旋轉 90 度,我還想過將行轉為列,將列轉為行,我曾想過先旋轉外側,內側和中間保持不變,因為它應該是 5x5。 無論如何,我不明白如何正確地做到這一點。
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];
}
}
}
我想通過行,創建一個臨時變量,然后當我到達該列時,我將用臨時變量替換它,循環應該繼續。 你怎么說?
暗示:
如果您想就地執行旋轉,您會注意到數據移動是四向交換,例如:
M[i,j] -> M[n+1-j,i] -> M[n+1-i,n+1-j] -> M[j,n+1-i] -> M[i,j]
我創建了一個算法來將矩陣向右旋轉 90°。 如果你想向左旋轉,你可以簡單地向右旋轉 3 次(當然如果你不關心性能:))。 該算法采用 MxN 矩陣。 如果您只需要旋轉 NxN 矩陣,那么您可以就地進行。 為簡單起見,我沒有在算法中包含這種情況。
我使用String
作為矩陣原始類型,以便我們可以更好地看到輸出單元格。 當然,你可以用int
作為基本類型來做同樣的事情。
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
}
}
輸出是:
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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.