【LeetCode】解题48:Rotate Image

LeetCode 48. Rotate Image 时间复杂度(O(n)) 时间复杂度(O(n)),思想:细心,耐心 class Solution: def rotate(self, matrix: [[int]]) -> None: n = len(matrix) for i in range(n // 2): for j in range(i, n - i - 1): ... 阅读详情

Problem 48: Rotate Image [Medium]

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

Rotate the image by 90 degrees (clockwise).

Note:

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

Given input matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],

rotate the input matrix in-place such that it becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
]

Example 2:

Given input matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],

rotate the input matrix in-place such that it becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]

来源:LeetCode

解题思路

将数组看作一个由N/2个正方形圈组成的套圈,每一次循环都将一个圈在原地顺时针旋转90°,能够保证不会重复旋转,并且不需要额外数组空间。
如下图所示:
在这里插入图片描述
一次旋转圈1:
在这里插入图片描述
第二次旋转圈2:
在这里插入图片描述
具体过程:

  • 循环N/2次,每次旋转的正方形的四个顶点为:
    t o p _ l e f t = [ s i d e , s i d e ] , top\_left = [side, side], top_left=[side,side]
    t o p _ r i g h t = [ s i d e , N − s i d e − 1 ] , top\_right = [side, N-side-1], top_right=[side,Nside1]
    b o t t o m _ r i g h t = [ N − s i d e − 1 , N − s i d e − 1 ] , bottom\_right = [N-side-1, N-side-1], bottom_right=[Nside1,Nside1]
    b o t t o m _ l e f t = [ N − s i d e − 1 , s i d e ] bottom\_left = [N-side-1, side] bottom_left=[Nside1,side]
  • 旋转正方形时需要调换 4 ∗ ( N − 2 ∗ s i d e − 1 ) 4*(N-2*side-1) 4(N2side1)个点的位置。每四个点为一组,内部进行旋转替换,循环该过程 ( N − 2 ∗ s i d e − 1 ) (N-2*side-1) (N2side1)次。
    分组如图所示:
    在这里插入图片描述

运行结果:
在这里插入图片描述

Solution (Java)

class Solution {
    public void rotate(int[][] matrix) {
        int N = matrix.length;
        if(N == 0 || N == 1) return;
        for(int i = 0; i < N / 2; i++){
            rotate_sides(matrix, i, N);
        }
    }
    private void rotate_sides(int[][] matrix, int side, int N){
        for(int i = side; i < N - side - 1; i++){
            int top = matrix[side][i];
            int right = matrix[i][N-side-1];
            int bottom = matrix[N-side-1][N-i-1];
            int left = matrix[N-i-1][side];
            matrix[i][N-side-1] = top;
            matrix[N-side-1][N-i-1] = right;
            matrix[N-i-1][side] = bottom;
            matrix[side][i] = left;
        }
    }
}
leetcode:Rotate Image菜鸟解法 这题想了很久,实在是找不到既要在原位,而且又很快速的方法,如果是matlab就好了...于是采用了最笨的一次把可能被影响到的四个点都搞定的方法,代码见下面 class Solution { public: void rotate(vector > &matrix) { int n = matrix.size(); int temp; 阅读详情

相关推荐

Rotate Image - LeetCode

目录 题目链接 注意点 解法 小结 题目链接 Rotate Image - LeetCode 注意点 不能开新的二维数组 解法 解法一:先以对角线为轴对调数字,在将每一行逆序即可。时间复杂度O(n^2) class Solution { public: v...

weixin_30594001的博客 140

[LeetCode]48. Rotate Image

48. Rotate Image 一、题目 Problem Description: You are given an nxnn x nnxn 2D matrix representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT

KKKKKKOBE_24的博客 707

Leetcode解题报告:48. Rotate Image

题意: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). 难度:Medium 解题思路一: 顺时针旋转90°,其实就是第i列倒序变成第 i行,很容易想到先把原矩阵复制到一个temp矩阵中,然后再遍历一次,把temp中的列按倒序

wengyunpeng的博客 265

LeetCode典型题目系列 48. Rotate Image 旋转图像

LeetCode典型题目系列 48. Rotate ImageLeetCode典型题目系列题目 48. Rotate Image思路编程技巧Code (Java) LeetCode典型题目系列 作为一名学计算机的学生,之前一直少有练习OJ,写博客,维护自己的GitHub的觉悟,说到底还是太懒了。春招以来愈发觉得练习OJ的重要性,于是准备写一些博客,记录自己遇到的典型题目和思考,一方面是给自己的笔记...

是阿福呀 594

48. Rotate Image**

48. Rotate Image** https://leetcode.com/problems/rotate-image/ 题目描述 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: You have to rotate the ima...

珍妮的选择的博客 230

Leetcode 48. Rotate Image (python)

Leetcode 48. Rotate Image题目解法:follow up1: 逆时针旋转90度follow up 2:旋转180度 题目 解法: 先transpose,然后把每行reverse class Solution: def rotate(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-place instead.

qq_37821701的博客 677

leetcode 48 Rotate Image

''' leetcode 48 Rotate Image 给定n*n的二维图像,代表一张图像,将原始图像顺时针旋转90度 必须是in-place的操作,不允许额外分配内存空间再旋转二维数组 给定输入矩阵: [ [1,2,3], [4,5,6], [7,8,9] ], 希望输出 [ [7,4,1], [8,5,2], [9,6,3] ] 算法1: 首先对原始的二维矩...

WYXHAHAHA123的博客 193

LeetCode | 48. 旋转图像 (Rotate Image)

链接:https://leetcode.cn/problems/rotate-image/solutions/1228078/48-xuan-zhuan-tu-xiang-fu-zhu-ju-zhen-yu-jobi/ 来源:力扣(LeetCode) 著作权归作者所有。输入: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]]

Lynn0_Y的博客 456

LeetCode:48. Rotate Image

LeetCode:48. Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: You have to rotate the image in-place, which means you have to mo...

Wisimer 171

LeetCode 48. Rotate Image 解题报告

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?.示例Example 1: Input: 1 2 4

骆小坑的填坑之旅 910

Leetcode48.Rotate Image旋转图像.java

题目 给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ] 示例 2: 给定 matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [

梦晨涌京 287

LeetCode: 48. Rotate Image

1. 原题链接 https://leetcode.com/problems/rotate-image/description/ 2. 题目要求 给定一个由 n*n 的二维数组 matrix[ ] [ ] 构成的矩阵,将这个矩阵顺时针方向旋转90度,并输出。如下图所示 3. 解题思路 首先对每一行进行交换,得到上图中的中间结果; 然后再交换 关于对角线对称的元素:matri...

aysk1112的博客 159

Leetcode48. 旋转图像(Rotate Image

Leetcode - 48 Rotate Image (Medium) 题目描述:将一个 n × n 的矩阵旋转 90°,不能使用额外的辅助空间。 Given input matrix = [ [1,2,3], [4,5,6], [7,8,9] ], rotate the input matrix in-place such that it becomes: [ [7,4,1]...

厉兵秣码 336

LeetCode 48 Rotate Image 解题思路和python代码

reverse矩阵中每一行的 time complexity 也是 O(n^2)。转置矩阵的 time complexity 是O(n^2),整体的 time complexity 是O(n^2)。接着,把 matrix 中的每一行都reverse。space complexity 是 O(1)。完成将 matrix 旋转90°。首先,转置这个matrix。

weixin_57266891的博客 765

leetcode 48. Rotate Image-旋转图像|python

旋转图像,这是一个在leetcoed上十分有意思的例子,也具有一定的实用性,尤其在图像处理领域应用较多,在leetcode上也比较受欢迎。在这一篇分享当中,会在解答本题的顺便分享一下解题过程当中的几个知识点。首先看看题目描述:给定一个 n × n 的二维矩阵表示一个图像。将图像顺时针旋转 90 度。说明:你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。示...

m0_37985723的博客 887

48. Rotate Image

题目来源: https://leetcode.com/problems/rotate-image/ 自我感觉难度/真实难度:hard 写题时间时长:1h 题意: 旋转一个矩阵 分析: 我自己打算用旋转最外圈的元素来实现旋转,内圈也是。但是这样很容易出错,对圈数和行数之间的关系,很容易出错 自己的代码: class Solution: ...

weixin_30614109的博客 104

leetcode解题48. Rotate Image java版(旋转图像)

leetcode解题48. Rotate Image java版(旋转图像)

mine_song的博客 2452

LeetCode 48.Rotate Image & 49.Group Anagrams

Problem 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? 解题思路: 方法1: 1. 如果不是直接

Ooooopsy的博客 348
上一篇: 【LeetCode】解题45:Jump Game II
下一篇: 【LeetCode】解题53:Maximum Subarray
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值