https://leetcode.com/problems/rotate-image/
解题思路: 要让矩阵顺时针旋转90度,只需先让矩阵沿着对角线交换,然后再每行逆序就可以得到
class Solution {
public void rotate(int[][] matrix) {
if(matrix.length <=1)return;
for(int i =0;i<matrix.length;i++){
for(int j=i;j<matrix.length;j++){
swap2D(matrix,i,j,j,i);
}
}
for(int i=0;i<matrix.length;i++){
reverse(matrix[i]);
}
}
private void reverse(int[] A){
if(A.length == 0)return;
int start = 0;
int end = A.length-1;
while(start < end){
swap(A,start,end);
start++;
end--;
}
}
private void swap(int[] A,int i,int j){
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
private void swap2D(int[][] A,int xi,int xj,int yi,int yj){
int temp = A[xi][xj];
A[xi][xj]= A[yi][yj];
A[yi][yj] = temp;
}
}