Skip to content

Instantly share code, notes, and snippets.

@hilda8519
Created June 18, 2014 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hilda8519/ed4f00a6b57be514e9a6 to your computer and use it in GitHub Desktop.
Save hilda8519/ed4f00a6b57be514e9a6 to your computer and use it in GitHub Desktop.
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