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;
}
}
Welcome Message
Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.
Top Navigation Menu
Showing posts with label Rotate Matrix. Show all posts
Showing posts with label Rotate Matrix. Show all posts
December 28, 2009
May 27, 2009
Program to rotate a matrix
//for(int i=0; i less than n-1 ; i++){
//for(int j=0;jless than n-i-1;j++){
//int temp = a[n-j][n-i];
//a[n-j][n-i] = a[i][j];
//a[i][j] =temp;
//}
//}
//for(int j=0;jless than n-i-1;j++){
//int temp = a[n-j][n-i];
//a[n-j][n-i] = a[i][j];
//a[i][j] =temp;
//}
//}
Labels:
Code,
java,
Rotate Matrix
Subscribe to:
Posts (Atom)