LeetCode 048 Rotate Image

题目


You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:
Could you do this in-place?

把图像顺时针旋转90°


思路


方法1:
用一个辅助数组放置数据
缺点:需要额外n*n的空间,需要n^2的时间

方法2:
把新的数组的坐标列出,直接按照公式来写
优点:普遍适用,容易想到。
缺点:数组下标混乱,每次要进行4个数据的移位转换,只需要一个temp


方法3
整体的通过数组找出关系
先把原数组转置,然后把每一行reverse
优点:
模块化 :转置模块,swap模块。

代码


public class Solution {
    public void rotate(int[][] matrix) {
        int n= matrix.length;
        
        for(int i=0;i<n;i++){
        	for(int j=i+1;j<n;j++){
        		int temp=matrix[i][j];
        		matrix[i][j]=matrix[j][i];
        		matrix[j][i]=temp;
        	}
        	reverse1(matrix[i]);
        		
        }
    }

	private void reverse1(int[] a) {
		for(int i=0;i<a.length/2;i++){
			int temp=a[i];
    		a[i]=a[a.length-1-i];
    		a[a.length-1-i]=temp;
		}
	}
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值