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]
]
思路

  题目要求我们只能再本地进行旋转,不能设置辅助空间。这道题在最开始做的时候,我先在草稿纸上画出交换的过程,然后从中找出规律。发现首先对于循环次数,当n是基数的时候,最里面的一层只有一个元素,不用进行旋转,所以循环的次数应该是 start < len(nums)//2.另外在元素交换的规律上,每一列有几个元素,我们就循环几次。意思是每一次循环交换的是将所有相应位置的元素进行交换。
  时间复杂度为O(mlogm), 其中m为矩阵一行的长度。空间复杂度为O(1)。
图解步骤


解决代码


 class Solution(object):
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: None Do not return anything, modify matrix in-place instead.
"""
n = len(matrix)
for l in range(n // 2): # 循环条件
r = n -1-l # 右边开始的位置
for p in range(l, r): # 每一层循环次数
q = n -1- p # 每一次循环交换时底部元素的小标
cache = matrix[l][p] # 对元素进行交换。
matrix[l][p] = matrix[q][l]
matrix[q][l] = matrix[r][q]
matrix[r][q] = matrix[p][r]
matrix[p][r] = cache

【LeetCode每天一题】Rotate Image(旋转矩阵)的更多相关文章

  1. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  2. leetcode第37题--Count and Say

    题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...

  3. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  4. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  5. LeetCode的刷题利器(伪装到老板都无法diss你没有工作)

    在工程效率大行其道的今天,如果不会写点代码以后也不容易在测试圈混下去.今天给大家推荐一个LeetCode的刷题利器,可以伪装到连你老板在这里走过去都无法确认你是在干活呢,还是在干活呢. LeetCod ...

  6. LeetCode每天一题之两数之和

    这个LeetCode刷题系列的博客权当是为自己记一下笔记吧.博客系列会从LeetCode的第一题开始刷,同时会从零开始学习[因为我就是零/(ㄒoㄒ)/~~].同时,如果有写错的地方,希望大佬们在评论区 ...

  7. leetcode第三题

    leetcode第三题: 题目: 给定一个字符串,找出不含有重复字符的最长子串的长度. 源码(使用java语言): class Solution { public int lengthOfLonges ...

  8. [LeetCode] 系统刷题5_Dynamic Programming

    Dynamic Programming 实际上是[LeetCode] 系统刷题4_Binary Tree & Divide and Conquer的基础上,加上记忆化的过程.就是说,如果这个题 ...

  9. LeetCode 第 342 题(Power of Four)

    LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether ...

  10. leetcode 入门第一题 4ms? 8ms? Two Sum

    今天开启leetcode 入门第一题 题意很简单,就是一个数组中求取两数之和等于目标数的一对儿下标 1.暴力 n^2 两个for循环遍历 用时0.1s 开外 代码就不用写了 2.二分 nlogn 我们 ...

随机推荐

  1. java编程感悟02

    很多时候我们需要网上查阅他人的代码,如果代码比较长或者注释较少,阅读起来就会比较费劲,这时需要培养快速读懂应用他人的代码的能力. 实现图片的旋转功能: import java.awt.geom.Aff ...

  2. Smarty模板保留缓存

    <?php //缓存 //注:使用缓存需要用到这几个方法: //(ob_start(开启内存缓存); ob_flush(清除内存缓存);) //file_exists这个方法是判断文件是否存在 ...

  3. Android编译系统入门(二)

    Android.mk的使用方法 在上一篇Android编译系统入门(一)中我们只要介绍了Android系统使用make命令默认编译的依赖树是droid,而droid是一个伪目标,它有两个先决条件dro ...

  4. 【转】asp.net项目在IE11下出现“__doPostBack”未定义的解决办法

    最近我们运营的网站有用户反馈在 IE 11 下<asp:LinkButton> 点击出现 "__doPostBack"未定义",经过一番google,终于知道 ...

  5. Nodejs----登录验证

    1. 写在前面 当我们登录了一个网站,在没有退出登录的情况下,我们关闭了这个网站 ,过一段时间,再次打开这个网站,依然还会是登录状态.这是因为,当我们登录了一个网站,服务器会保存我们的登录状态,直到我 ...

  6. SQL多结果集导出Excel

    由于本项目工作中需要,有时会导出一些数据给客户,但又不是每次都需要,可能这次用了下次可能就不会使用,导出数据,我们正在做的一个项目中与四川地区有关,所以导出数据就有如下需求: 1.  按各市导出数据, ...

  7. 洛谷 P1090合并果子【贪心】【优先队列】

    题目描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可 ...

  8. F#周报2018年第52期

    新闻 Sudokube--使用Fable开发的数独立方体 Rust 2019年及以后的发展 视频及幻灯片 我爱F#代码 马蒂亚斯·布兰在Developer On Fire上的演讲--有条理的和有趣的 ...

  9. pycharm 下的djiango使用

    创建工程可以在虚拟环境下运行,创建工程后使用命令 在python 下的命令窗口(Terminal) python3 manage.py startapp django_web (或者 python3替 ...

  10. Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...