# 题目描述

给定 matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],

[
[7,4,1],
[8,5,2],
[9,6,3]
]

给定 matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],

[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]

# 解题思路

1.转置矩阵：swap(matrix[i][j], matrix[j][i])

1  2  3            1  4  7
4  5  6   ---->    2  5  8
7  8  9            3  6  9

2.水平翻转矩阵：swap(matrix[i][j], matrix[i][matrix.length-1-j]

1  4  7            7  4  1
2  5  8   ---->    8  5  2
3  6  9            9  6  3
    public void rotate(int[][] matrix) {
//转置矩阵，swap(matrix[i][j], matrix[j][i])
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;
}
}
//水平翻转矩阵，swap(matrix[i][j], matrix[i][matrix.length-1-j]
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;
}
}
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120