a1 a2 a3
b1 b2 b3
c1 c2 c3
行交换
c1 c2 c3
b1 b2 b3
a1 a2 a3
然后对角交换
c1 b1 a1
c2 b2 a2
c3 b3 a3
matrix[0][1] matrix[0][2]
matrix[1][2]
注意理解对角交换的代码,只处理右上三角的区域 c2 c3 b3
for(int i = 0; i < matrixRowSize; i++)
for(int j = i+1; j < matrixColSize; j++){
int tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
void swap_row(int **a, int up, int down, int col){
while(up<down){
int tmp = a[up][col];
a[up][col] = a[down][col];
a[down][col] = tmp;
up++;
down--;
}
}
void rotate(int** matrix, int matrixRowSize, int matrixColSize) {
for(int i = 0; i < matrixColSize; i++)
swap_row(matrix, 0, matrixRowSize-1,i);
for(int i = 0; i < matrixRowSize; i++)
for(int j = i+1; j < matrixColSize; j++){
int tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}