Rotate a 2d matrix to the right
Posted
by adam
on Stack Overflow
See other posts from Stack Overflow
or by adam
Published on 2010-04-25T08:16:03Z
Indexed on
2010/04/25
8:23 UTC
Read the original article
Hit count: 345
I want a 2d matrix to rotate to the right, it compiles fine but when I try to the run it freezes. For example I want {{10,20,30},{40,50,60}} to rotate into {{40,10},{50,20},{60,30}}
import java.util.*;
public class Rotate{
public static int[][] rotate(int[][] m) {
int [][] rotateM = new int[m[0].length][m.length];
for (int i= 0; i< m.length; i= i++){
for (int j= 0; j< m[0].length; j= j++){
rotateM[i][j] = m[j][m.length-i-1];
}
}
return rotateM;
}
public static void main(String[]args){
int[][]m = {{10,20,30},
{40,50,60}};
System.out.println(Arrays.toString(rotate(m)));
}
}
© Stack Overflow or respective owner