Skip to content

Instantly share code, notes, and snippets.

@asdw3276
Last active August 29, 2015 14:02
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 asdw3276/a4f9223bb5f678e50cbb to your computer and use it in GitHub Desktop.
Save asdw3276/a4f9223bb5f678e50cbb to your computer and use it in GitHub Desktop.
1.6
public class rotateimage{
public static void main(String[] args)
{
int[][] matrix ={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16 ,17 ,18, 19, 20},{21 ,22 ,23 ,24, 25}};
matrix = rotateimage(matrix);
for(int i = 0; i < matrix.length; i++)
{
System.out.println();
for(int j = 0; j < matrix[0].length; j++)
{
System.out.print(matrix[i][j] + ",");
}
}
}
public static int[][] rotateimage(int[][] matrix)
{
if(matrix == null)
return null;
int temp = 0;
int n = matrix.length;
if(n <= 1)
return matrix;
for(int i = 0; i < n / 2; i++)
for(int j = 0; j <= n / 2; j++)
{
if(n % 2 == 0 && j == n / 2)
continue;
temp = matrix[i][j];
matrix[i][j] = matrix[n - 1 - j][i];
matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
matrix[j][n - 1 - i] = temp;
}
return matrix;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment