LeetCode(cai鸟之路)48 旋转图像

题目描述

给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。

示例

在这里插入图片描述
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[[7,4,1],[8,5,2],[9,6,3]]
在这里插入图片描述
输入:matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
输出:[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

代码:

class Solution {
   public static void rotate(int[][] matrix) {
        int[][] temp = new int[matrix.length][matrix.length];
        List<List<Integer>> listList = new ArrayList<>();
        for (int i = 0;i<matrix.length;i++){
            List<Integer> integerList = new ArrayList<>();
            for (int j = 0;j<matrix.length;j++){
                integerList.add(matrix[i][j]);
            }
            listList.add(integerList);
        }
        for (int i = 0;i<matrix.length;i++){
            List<Integer> integerList = listList.get(temp.length -i-1);
            for (int k= 0;k<matrix.length;k++){
                matrix[k][i] = integerList.get(k);
            }
        }
    }
}

分析

在这里插入图片描述
从上图中可以看出,规律来,就是行变成了列,如第一行则变成最后一列。
这就好办了,我们可以将每行提取出来,然后再依次竖着进行排列就行。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rotate-image

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值