Leetcode刷题记——48. 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?

Subscribe to see which companies asked this question.

给一个n*n的数组矩阵代表一个2D图像,顺时针旋转90度,得到的图像

二、解题思路:

Medium题,简单举个例子即可得出规律。

如:

[2,5                     [8,  2              

8,,4]     旋转后为 4,5]  即,旋转后的第一行为旋转前第一列的逆序;第二行为第二列的逆序。。。以此类推。

(1)创建中间二维数组res,按上述规律为res赋值。

(2)将res内容复制回原数组即可。

三、源码:

import java.util.ArrayList;  
import java.util.Arrays;
import java.util.List;  
  
public class Solution  
{  
	public void rotate(int[][] matrix) 
	{
		int n = matrix.length;
		if (n == 0) return;
		int [][] res = new int[n][n];
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				res[i][j] = matrix[n-j-1][i];
		
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				matrix[i][j] = res[i][j];
		System.out.print(Arrays.deepToString(matrix));
		return;
    }
    public static void main(String args[])      
    {      
              
    	//String a = "";
    	//String b = "";
      //  int[] digits = {0};      
        Solution solution = new Solution();      
        int[][] abc = {{2}};  
      //  int[] b = {2,3,4};  
       // for(int i = 0; i < abc.length; i ++)      
        solution.rotate(abc);
   //       System.out.print(solution.rotate(abc));      
          
    }      
}  


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值