顺时针旋转阵列
我有一个二维数组,我需要顺时针旋转90度,但是我不断得到数组索引出界...
public int[][] rotateArray(int[][] arr) {
// first change the dimensions vertical length
// for horizontal length and vice versa
int[][] newArray = new int[arr[0].length][arr.length];
// invert values 90 degrees clockwise by starting
// from button of array to top and from left to right
int ii = 0;
int jj = 0;
for (int i = 0; i < arr[0].length; i++) {
for (int j = arr.length - 1; j >= 0; j--) {
newArray[ii][jj] = arr[i][j];
jj++;
}
ii++;
}
return newArray;
}