Created
January 8, 2014 16:49
-
-
Save naveenwashere/8319992 to your computer and use it in GitHub Desktop.
Rotate a Matrix by +180 degrees.
This file contains hidden or 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 com.interview.coding.crack; | |
public class RotateMatrixByOneEightyDegree { | |
public void rotateByOneEightyDegree(int[][] array, int m, int n) | |
{ | |
for(int i = 0; i < ((m/2) + (m%2)); i++) | |
{ | |
int first = i; | |
int last = n - 1; | |
for(int j = 0; j < n; j++) | |
{ | |
int offset = last - i; | |
int top = array[first][j]; | |
array[first][j] = array[offset][last - j]; | |
array[offset][last - j] = top; | |
} | |
} | |
} | |
public void printArray(int[][] array, int m, int n) | |
{ | |
for(int i = 0; i < m; i++) | |
{ | |
for(int j = 0; j < n; j++) | |
{ | |
System.out.print(array[i][j] + "\t"); | |
} | |
System.out.println(); | |
} | |
System.out.println("|--------------------------------------|"); | |
} | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
RotateMatrixByOneEightyDegree rotateBy180 = new RotateMatrixByOneEightyDegree(); | |
int two[][] = new int[][]{{1,2}, {3,4}}; | |
int three[][] = new int[][]{{1,2,3}, {4,5,6}, {7,8,9}}; | |
int four[][] = new int[][]{{1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16}}; | |
int five[][] = new int[][]{{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}}; | |
int six[][] = new int[][]{{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,26,27,28,29,30}, {31,32,33,34,35,36}}; | |
System.out.println("2x2 Array"); | |
rotateBy180.printArray(two, 2, 2); | |
System.out.println("2x2 Array - 180*degree"); | |
rotateBy180.rotateByOneEightyDegree(two, 2, 2); | |
rotateBy180.printArray(two, 2, 2); | |
System.out.println(); | |
System.out.println("3x3 Array"); | |
rotateBy180.printArray(three, 3, 3); | |
System.out.println("3x3 Array - 180*degree"); | |
rotateBy180.rotateByOneEightyDegree(three, 3, 3); | |
rotateBy180.printArray(three, 3, 3); | |
System.out.println(); | |
System.out.println("4x4 Array"); | |
rotateBy180.printArray(four, 4, 4); | |
System.out.println("4x4 Array - 180*degree"); | |
rotateBy180.rotateByOneEightyDegree(four, 4, 4); | |
rotateBy180.printArray(four, 4, 4); | |
System.out.println(); | |
System.out.println("5x5 Array"); | |
rotateBy180.printArray(five, 5, 5); | |
System.out.println("5x5 Array - 180*degree"); | |
rotateBy180.rotateByOneEightyDegree(five, 5, 5); | |
rotateBy180.printArray(five, 5, 5); | |
System.out.println(); | |
System.out.println("6x6 Array"); | |
rotateBy180.printArray(six, 6, 6); | |
System.out.println("6x6 Array - 180*degree"); | |
rotateBy180.rotateByOneEightyDegree(six, 6, 6); | |
rotateBy180.printArray(six, 6, 6); | |
System.out.println(); | |
} | |
} |
Yes, this is exactly the break I was telling you about :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is what I actually imagined when I told you the answer.