题意:
解题思路:
思路:两次交换
1. 上下交换 00 => 20, 01 => 21, 02 => 22
2. 右斜线交换 00 => 00, 01 => 10, 02 => 20
PHP代码实现:
class Solution {
function rotate(&$matrix) {
$m = count($matrix);
$n = count($matrix[0]);
//上下交换. 00=>20, 01 => 21, 02 => 22
for ($i = 0; $i < floor($m / 2); $i ++) {
for ($j = 0; $j < $n; $j++) {
$temp = $matrix[$i][$j];
$matrix[$i][$j] = $matrix[$m - $i - 1][$j];
$matrix[$m - $i - 1][$j] = $temp;
}
}
//右斜线交换. 00=>00, 01 => 10, 02 => 20
for ($i = 0; $i < $m; $i++) {
for ($j = $i; $j < $n; $j++) {
$temp = $matrix[$i][$j];
$matrix[$i][$j] = $matrix[$j][$i];
$matrix[$j][$i] = $temp;
}
}
}
}
GO代码实现:
func rotate(matrix [][]int) {
m := len(matrix)
n := len(matrix[0])
for i := 0; i < m / 2; i++ {
for j := 0; j < n; j++ {
matrix[i][j], matrix[m - i - 1][j] = matrix[m - i - 1][j], matrix[i][j]
}
}
for i := 0; i < m; i++ {
for j := i; j < n; j++ {
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
}
}
}