public class RotateMatrix {
//~ Methods --------------------------------------------------------------------------------------------------------
/**
* @param args
*/
public static void main(String[] args) {
int[][] mat = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
int[][] flipped = rotate(mat);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(" " + flipped[i][j]);
}
System.out.println();
}
}
public static int[][] rotate(int[][] inMatrix) {
int[][] outMatrix = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
outMatrix[i][j] = inMatrix[j][i];
}
}
return outMatrix;
}
}
No comments:
Post a Comment