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. Oracle数据库入门——基础知识

    1.安装完成Oracle数据库后,使用sqlplus客户端登录数据库管理系统,只输入用户名,没有输入密码时,会提示口令为空,登录被拒绝. 请输入用户名:system 输入口令: ERROR:ORA-0 ...

  2. 10.24CRM完成

    2018-10-24 16:16:59 已经完成了crm项目: 项目做完了,这几天可以安静整理Django啦!!!把博客整理一下! 然后再过几天针就可以回学校啦!! 今天程序员节  节日快乐!hell ...

  3. rxjs 常用的静态操作符

    操作符文档 API create const { Observable } = require('rxjs'); // 创建 Observables var observable = Observab ...

  4. ABP之展现层(Datatables分页)

    在前面的随笔中,已经介绍了ABP的增删改查的操作,但是对于查询的数据并没有进行分页,只是进行粗糙的展示,今天的随笔中将摸索进行分页展示.这里打算使用的分页插件是DataTables,这是一款比较强大的 ...

  5. 数据库系统Informix为例,介绍改善用户查询计划的方法。

    数据库系统Informix为例,介绍改善用户查询计划的方法. 1.合理使用索引 索引是数据库中重要的数据结构,它的根本目的就是为了提高查询效率.现在大多数的数据库产品都采用IBM最先提出的ISAM索引 ...

  6. .NET Core 2.0 单元测试中初识 IOptionsMonitor<T>

    在针对下面设置 CookieAuthenticationOptions 的扩展方法写单元测试时遇到了问题. public static IServiceCollection AddCnblogsAut ...

  7. eclipse安装Hadoop-0.20.2插件

    因为在使用Hadoop-0.20.2这个古董,需要使用它自带的eclipse插件,而我最初安装的是现代的eclipse4.10.0. 在经历两天,以及以下种种尝试之后,均以失败告终: 1.网上找适合的 ...

  8. [No0000103]JavaScript-基础课程3

    在 JavaScript 中,函数的参数是比较有意思的,比如,你可以将任意多的参数传递给一个函数,即使这个函数声明时并未制定形式参数 function adPrint(str, len, option ...

  9. day6:前两小节补充

    1,练习题一:以66分割,大于部分一个键值对,小于部分一个键值对 li = [23,78,67,45,34,89,67,78,23,23] lig = [] lil = [] dic = {} for ...

  10. [skill][vim] 常用技巧与配置

    一:  光标行列高亮 可以使用 :help highlight 查看相信帮助可颜色配置. set cursorline set cursorcolumn highlight Cursorline ct ...