给出一个用二维矩阵表示的图像
返回该图像顺时针旋转90度的结果
扩展:
你能使用原地算法解决这个问题么?
//注意调节好参数,不然就错一个数字也要调试好久 //因为旋转矩阵的时候如果仅交换两个值会有覆盖现象 //所以从容易理解的逻辑出发,需要额外存储空间暂存值,运行时间57ms空间11372k public void rotate(int[][] matrix) { int[] arr = new int[matrix.length]; //存矩形的上边(上面行) int[] arr1 = new int[matrix.length]; //存矩形的右边 if (matrix.length == 1) { return; } // 看作多个嵌套旋转的四边框,左上角坐标(i,i) for (int i = 0; i <= (matrix.length - 1) / 2; i++) { if((i==(matrix.length - 1) / 2)&&(matrix.length%2>0)){ break; } // 储存上面行 int count = 0; int nailIndex = matrix.length - 1 - i; for (int up = i; up <= nailIndex; up++) { arr[count] = matrix[i][up]; count++; } // 储存右边行 int count1 = 0; for (int rightIn = i; rightIn <= nailIndex; rightIn++) { arr1[count1] = matrix[rightIn][nailIndex]; count1++; } // 左行盖上面行 int leftTemp=i; for (int left = nailIndex; left >= i; left--) { matrix[i][left] = matrix[leftTemp][i]; leftTemp++; } // 下面换到左边 for (int down = i; down <= nailIndex; down++) { matrix[down][i] = matrix[nailIndex][down]; } // 储存的上边行覆盖右边行 count = 0; for (int rightSav = i ; rightSav <= nailIndex; rightSav++) { matrix[rightSav][nailIndex] = arr[count]; count++; } // 存储的右边换到下面 count1 = 0; for (int right = nailIndex; right >=i; right--) { matrix[nailIndex][right]=arr1[count1]; count1++; } } }
public class Solution { public void rotate(int[][] matrix) { int len = matrix.length; for (int i = 0; i < len; i++) { for (int j = 0; j < i; j++) { int tmp = matrix[j][i]; matrix[j][i] = matrix[i][j]; matrix[i][j] = tmp; } } for (int i = 0; i < len; i++) { for (int j = 0; j < len / 2; j++) { int tmp = matrix[i][j]; matrix[i][j] = matrix[i][len - j - 1]; matrix[i][len - j - 1] = tmp; } } } }
import java.util.*; public class Solution { public void rotate(int[][] matrix) { int len = matrix.length; int[][] copy = new int[len][matrix[0].length]; for(int i = 0; i < len; i++) copy[i] = Arrays.copyOf(matrix[i], matrix[i].length); for(int i = 0; i < len; i++){ for(int j = 0; j < len; j++){ matrix[j][len-i-1] = copy[i][j]; } } return; } }
public class Solution { public void rotate(int[][] matrix) { int n = matrix.length; for(int i=0;i<n/2;i++){ for(int j=i;j<n-i-1;j++){ int 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; } } } }
public class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
int temp = 0;
for (int i = 0; i < n / 2; i++) {
for (int j = i; j < n - 1 - i; j++) {
temp = matrix[i][j];
matrix[i][j] = matrix[n-j-1][i];
matrix[n-j-1][i] = matrix[n-i-1][n-j-1];
matrix[n-i-1][n-j-1] = matrix[j][n-i-1];
matrix[j][n-i-1] = temp;
}
}
}
}
public class Solution { public void rotate(int[][] matrix) { int n = matrix.length; for(int i=0; i<n/2; i++) for(int j=i; j<n-1-i; j++) { int tmp = 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] = tmp; } } }
public class Solution { public void rotate(int[][] matrix) { int n = matrix.length; for (int i = 0; i < n; i ++) { for (int j = 0; j < n - i; j ++) { matrix[i][j] = matrix[n - 1 - j][n - 1 - i] ^ matrix[i][j] ^ (matrix[n - 1 - j][n - 1 - i] = matrix[i][j]); } } for (int i = 0; i < n / 2; i ++) { for (int j = 0; j < n; j ++) { matrix[i][j] = matrix[n - 1 - i][j] ^ matrix[i][j] ^ (matrix[n - 1 - i][j] = matrix[i][j]); } } } }
The idea was firstly transpose the matrix and then flip it symmetrically. For instance,
1 2 3 4 5 6 7 8 9
after transpose, it will be swap(matrix[i][j], matrix[j][i])
1 4 7 2 5 8 3 6 9
Then flip the matrix horizontally. (swap(matrix[i][j], matrix[i][matrix.length-1-j])
7 4 1 8 5 2 9 6 3
Hope this helps.
public class Solution { public void rotate(int[][] matrix) { for(int i = 0; i<matrix.length; i++){ for(int j = i; j<matrix[0].length; j++){ int temp = 0; temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } for(int i =0 ; i<matrix.length; i++){ for(int j = 0; j<matrix.length/2; j++){ int temp = 0; temp = matrix[i][j]; matrix[i][j] = matrix[i][matrix.length-1-j]; matrix[i][matrix.length-1-j] = temp; } } } }