const arr = [
[1, 2, 3], //[7,4,1]
[4, 5, 6], //[8,5,2]
[7, 8, 9]] //[9,6,3]
function demo(arr) {
const n = arr.length
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
[arr[i][j], arr[j][i]] = [arr[j][i], arr[i][j]]
}
}
console.log(arr);
for (let i = 0; i < n; i++) {
for (let j = 0; j < Math.floor(n / 2); j++) {
[arr[i][j], arr[i][n - j - 1]] = [arr[i][n - j - 1], arr[i][j]]
}
}
console.log(arr);
}
demo(arr)
//先交换对角线
//左旋交换行 i行和n-i-1行
//优选交换列 j列和n-j-1行
06-17
1580
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-10
6万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-08
6002
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-19
3909
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)