博客域名:http://www.xnerv.wang

原标题页:https://oj.leetcode.com/problems/rotate-image/

题目类型:下标计算

难度评价:★★★

本文地址:http://blog.csdn.net/nerv3x3/article/details/37968757

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?

顺时针90度转动一个二维矩阵,要求in-place的算法,也就不是不能另外开一个新的矩阵然后把原矩阵上的元素一个个相应复制过来,而要在原矩阵上进行操作。

眼下能想到的有两种方法。

第一种比較直观,首先将原矩阵依照水平轴上下翻转,然后依照左上到右下的对角线再反转,则等价于90度顺时针转动。这种方法的缺点是要反转两次,长处是直观,不easy出错。

另外一种方法每一个元素仅仅须要调整一次,可是转化公式会比較复杂。对于矩阵matrix[n][n]中的某一点matrix[x][y](0<=x, y<n),90度顺时针转动后到了matrix[y][n-x-1],而matrix[y][n-x-1]到了matrix[n-x-1][n-y-1]。matrix[n-x-1][n-y-1]到了matrix[n-y-1][x],matrix[n-y-1][x]又到了matrix[x][y],可见这四个点正好实现了一次旋转互换。

本文採用另外一种方法。空间复杂度O(1)。时间复杂度O(n*n)。

class Solution:
# @param matrix, a list of lists of integers
# @return nothing (void), do not return anything, modify matrix in-place instead.
def rotate(self, matrix):
n = len(matrix)
if 1 == n:
return
round = int(n / 2)
for x in range(0, round):
for y in range(x, n - x - 1):
matrix[n - y - 1][x], matrix[n - x - 1][n - y - 1], matrix[y][n - x - 1], matrix[x][y] = matrix[n - x - 1][n - y - 1], matrix[y][n - x - 1], matrix[x][y], matrix[n - y - 1][x]

版权声明:本文博客原创文章,博客,未经同意,不得转载。

【LeetCode with Python】 Rotate Image的更多相关文章

  1. 【LeetCode with Python】 Sort List

    博客域名:http://www.xnerv.wang 原题页面:https://oj.leetcode.com/problems/sort-list/ 题目类型: 难度评价:★ 本文地址:http:/ ...

  2. 【leetcode 字符串处理】Compare Version Numbers

    [leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...

  3. 【LeetCode算法-27】Remove Element

    LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...

  4. 【head first python】2.共享你的代码 函数模块

    #coding:utf-8 #注释代码! #添加两个注释,一个描述模块,一个描述函数 '''这是nester.py模块,提供了一个名为print_lol()的函数, 这个函数的作用是打印列表,其中可能 ...

  5. 【LeetCode算法-9】Palindrome Number

    LeetCode第9题 Determine whether an integer is a palindrome. An integer is a palindrome when it reads t ...

  6. 【leetcode 桶排序】Maximum Gap

    1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...

  7. 【leetcode算法-简单】1.两数之和

    [题目描述] 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个 ...

  8. 【LeetCode刷题】——两数之和.1

    ---恢复内容开始--- 一直想在leetcode上面刷题,但是java刚刚摸了一下门,所以迟迟没有动手,今天做了第一道题,感觉自己实在菜的不行,但是还是学到了很多东西, 就记录一下遇到的问题. 首先 ...

  9. 【leetcode算法-简单】7.整数反转

    [题目描述] 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123输出: 321 示例 2: 输入: -123输出: -321 示例 3: 输入: 12 ...

随机推荐

  1. Swift - 数组排序方法(附样例)

    下面通过一个样例演示如何对数组元素进行排序.数组内为自定义用户对象,最终要实现按用户名排序,数据如下: 1 2 3 4 var userList = [UserInfo]() userList.app ...

  2. form表单多值提交

    $.ajax({ cache: true, type: "POST", url:ajaxCallUrl, data:$('#yourformid').serialize(),// ...

  3. Codeforces Round #257 (Div. 2) B Jzzhu and Sequences

    Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...

  4. “ASP.default_aspx”并不包括“DropDownList1_SelectedIndexChanged”的定义,其解决方法。

    "ASP.default_aspx"并不包括"DropDownList1_SelectedIndexChanged"的定义,其解决方法. 在使用DropDown ...

  5. xcode5下一个ffmpeg静态库配置

    1.若要安装xcode命令行工具 1).xcode5安装命令行工具方法: 在终端运行命令Using xcode-select --install 2).xcode5之前安装命令行工具方法: 2.xco ...

  6. hdu 2147 SG函数打表(手写也可以) 找规律

    kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/1000 K (Java/Others) Total ...

  7. 3TB硬盘的容量已经超出了传统分区标准的支持

    为什么3TB会有接近750G空间不能用? MBR分区格式是瓶颈 其实3TB硬盘之所以会出现各种问题,关键就在于它的容量已经超出了传统分区标准的支持.传统的硬盘采用MBR分区格式,使用LBA寻址,这种寻 ...

  8. Mysql InnoDB 是IOT表 锁基于索引

    </pre>Mysql InnoDB 是IOT表 锁基于索引<pre>

  9. POJ 1562 Oil Deposits

    转载请注明出处:http://blog.csdn.net/a1dark 大规模的图论切题之旅正式开始了.由于今天停了一天的电.所以晚上才开始切题.直到昨晚才把图论大概看了一遍.虽然网络流部分还是不怎么 ...

  10. 表达式树动态拼接lambda

    动态拼接lambda表达式树   前言 最近在优化同事写的代码(我们的框架用的是dapperLambda),其中有一个这样很普通的场景——界面上提供了一些查询条件框供用户来进行过滤数据.由于dappe ...