Created
June 18, 2014 22:55
-
-
Save hilda8519/ed4f00a6b57be514e9a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rotateMatrix; | |
import java.util.*; | |
public class rotateMatrix { | |
public static int[][] rotateMat(int[][] matrix){ | |
int n=matrix.length; | |
for(int row=0;row<n/2;row++){ | |
int first=row; | |
int last=n-1-row; | |
for(int i=first;i<last;i++){ | |
int offset=i-first; | |
int top=matrix[first][i]; | |
matrix[first][i]=matrix[last-offset][first]; | |
matrix[last-offset][first]=matrix[last][last-offset]; | |
matrix[last][last-offset]=matrix[i][last]; | |
matrix[i][last]=top; | |
} | |
} | |
return matrix; | |
} | |
public static void main(String[] args){ | |
int[][]matrix={{1,2},{3,4},{5,6},{7,8}}; | |
matrix=rotateMat(matrix); | |
System.out.println(matrix); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment