var rotate = function(matrix) {
let left=0,right=matrix[0].length-1;
let top=0,bottom=matrix.length-1;
while(left<right&&top<bottom){
for(let i=left;i<right;i++){
let d=i-left;
let temp1=matrix[i][right];
matrix[i][right]=matrix[top][i];
let temp2=matrix[bottom][right-d];
matrix[bottom][right-d]=temp1;
let temp3=matrix[right-d][left];
matrix[right-d][left]=temp2;
matrix[top][i]=temp3;
}
left++,right--;
top++,bottom--;
}
};
算法核心:与前一道题思路相同,较为暴力
算法注意点:先确定偏移量d
(灵神有更"优雅"的解法:两次翻转等于一次旋转)

被折叠的 条评论
为什么被折叠?



