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:

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

Example 2:

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

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


  1. 解决代码

  1.  
  1. class Solution(object):
  2. def rotate(self, matrix):
  3. """
  4. :type matrix: List[List[int]]
  5. :rtype: None Do not return anything, modify matrix in-place instead.
  6. """
  7. n = len(matrix)
  8. for l in range(n // 2): # 循环条件
  9. r = n -1-l # 右边开始的位置
  10. for p in range(l, r): # 每一层循环次数
  11. q = n -1- p # 每一次循环交换时底部元素的小标
  12. cache = matrix[l][p] # 对元素进行交换。
  13. matrix[l][p] = matrix[q][l]
  14. matrix[q][l] = matrix[r][q]
  15. matrix[r][q] = matrix[p][r]
  16. 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. Python零基础入门学习 作者:小甲鱼

    temp = input('不妨想一想小甲鱼现在心里想的哪一个数字:') guess = int(temp) if guess == 8: print('你是小甲鱼心里的蛔虫吗?') print('哼 ...

  2. 文件下载报错:引发类型为“System.OutOfMemoryException”的异常-.Net 内存溢出

    CSDN:http://blog.csdn.net/huwei2003/article/details/53559272 设置了也没有用,于是想到手动清理应用程序池,但又迁配置问题于是改成最后的方式! ...

  3. shell之使用cut切割文本文件

    我们知道可以通过工具grep或egrep按行筛选记录,这里我们可以通过cut工具对文本按列进行切分,它可以指定定界符,linux下制表符是默认的定界符. #cut -f 2,3 textfile 这个 ...

  4. C和C指针小记(九)-指针用法1

    1. *p++ 最常用的一个指针的用法,就是在循环中用来迭代. *p++ 共有3步操作: 1.++操作符把p所指向的内存中的值复制一份 2.++操作符把p加1(实际是一个p所指内存单元的大小,这也是编 ...

  5. OC,nil,NULL,Nil,kCFNull

    YYModel源码中有一句:kCFNull //解析model属性并附值 + (instancetype)yy_modelWithDictionary:(NSDictionary *)dictiona ...

  6. Arcengine 在SDE创建数据集提示应用程序未获得创建或修改此类型数据的方案的许可

    解决方案:将license Control的属性修改一下,ArcGIS Engine的√去掉,把第二个打√就可以了: 参考资料:http://www.docin.com/p-925448534.htm ...

  7. py 正则表达式 List的使用, cxfreeze打包

    从index.html当做检索出压缩文件,index.html的内容如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN& ...

  8. 批处理DOS基础命令

    批处理(Batch),也称为批处理脚本.顾名思义,批处理就是对某对象进行批量的处理.批处理文件的扩展名为bat. 批处理文件,或称为批处理程序,是由一条条的DOS命令组成的普通文本文件,可以用记事本直 ...

  9. 【PyQt5-Qt Designer】QDoubleSpinBox-小数微调框

    QDoubleSpinBox-小数微调框 总体说明 大部分的总体说明和QSpinBox的差不多(详见:<PyQt5:微调框1>),这里主要把有差别的地方谈一下(三点). QDoubleSp ...

  10. SpringBoot-热部署Devtools

    热部署 什么是热部署 所谓的热部署:比如项目的热部署,就是在应用程序在不停止的情况下,实现新的部署 项目演示案例 @RestController @Slf4j public class IndexCo ...