【Java】【力扣】48.旋转图像
思路
就是找规律
具体:
先定义4边界
1. 先角落
2.后偏移 1 ,2,3...
这次4边界的循环完, 再移动边界
草稿版:
代码
class Solution {public void rotate(int[][] matrix) {//定义临时变量//2指针int temp=0;int left=0;int right= matrix.length-1;//外层循环,直到lr相遇结束while (left<right) {//定义2指针int top=left;int bottom=right;//内层循环:i为偏移量,开始值:0,次数:从0 到<right-left//交换for (int i = 0; i <right-left; i++) {temp=matrix[top][left+i];matrix[top][left+i]=matrix[bottom-i][left];matrix[bottom-i][left]=matrix[bottom][right-i];matrix[bottom][right-i]=matrix[top+i][right];matrix[top+i][right]=temp;}//内层结束:l r变化left++;right--;}}
}