博客域名: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. NumberFormat 类

    NumberFormat 表示数字的格式化类, 即:能够依照本地的风格习惯进行数字的显示. 此类的定义例如以下: public abstract class NumberFormat extends ...

  2. Lucene.Net 2.3.1开发介绍 —— 四、搜索(三)

    原文:Lucene.Net 2.3.1开发介绍 -- 四.搜索(三) Lucene有表达式就有运算符,而运算符使用起来确实很方便,但另外一个问题来了. 代码 4.3.4.1 Analyzer anal ...

  3. Lucene.Net 2.3.1开发介绍 —— 三、索引(五)

    原文:Lucene.Net 2.3.1开发介绍 -- 三.索引(五) 话接上篇,继续来说权重对排序的影响.从上面的4个测试,只能说是有个直观的理解了.“哦,是!调整权重是能影响排序了,但是好像没办法来 ...

  4. 无法安装vmware tools的解决方PLEASE WAIT! VMware Tools is currently being installed on your system. Dependin

    VMware安装unbuntu 12.04 LTS时,当你使用VMware的Easy Mode安装时,提示须要安装VMware Tools,屏幕会出现下方的文字: installed unbuntu ...

  5. 小记css的margin collapsing

    近期在做web页面设计的时候,莫名的发现最上面会出现一个横条,颜色为html的背景颜色.本意是那一片空横条应该为header的背景色.查了一些资料,发现是margin collapsing的问题,记录 ...

  6. 《转》Frameset布局

    前二天在写一个HTML界面,用到了Frameset,主要学习都是在下面的文章里,内容写得很详细,值得推荐大家看下. 网址:http://captaincook.iteye.com/blog/36563 ...

  7. [Windows Phone学习笔记]页面之间传递对象

    在Windows Phone中,页面之间传递参数就类似Web开发中一样,通过QueryString的形式进行传递,但是如果需要传递对象,则无法通过QueryString形式了,其实也可以,把对象序列化 ...

  8. Spring整合的quartz任务调度的实现方式

    一.在web.xml中将配置文件的位置指定好. Web.xml的配置如下: <?xmlversion="1.0"encoding="UTF-8"?> ...

  9. leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

    1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...

  10. [Cocos2d-x]创建项目

    从cocos2d-x 2.1.4之后,就不提供工程项目模板的安装文件了,我们只能手工修改以前的安装文件,让它连接最新的工程,但是这样很麻烦. 我们可以使用python命令创建cocos2d-x项目 步 ...