【LeetCode with Python】 Rotate Image
博客域名: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的更多相关文章
- 【LeetCode with Python】 Sort List
博客域名:http://www.xnerv.wang 原题页面:https://oj.leetcode.com/problems/sort-list/ 题目类型: 难度评价:★ 本文地址:http:/ ...
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
- 【LeetCode算法-27】Remove Element
LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...
- 【head first python】2.共享你的代码 函数模块
#coding:utf-8 #注释代码! #添加两个注释,一个描述模块,一个描述函数 '''这是nester.py模块,提供了一个名为print_lol()的函数, 这个函数的作用是打印列表,其中可能 ...
- 【LeetCode算法-9】Palindrome Number
LeetCode第9题 Determine whether an integer is a palindrome. An integer is a palindrome when it reads t ...
- 【leetcode 桶排序】Maximum Gap
1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...
- 【leetcode算法-简单】1.两数之和
[题目描述] 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个 ...
- 【LeetCode刷题】——两数之和.1
---恢复内容开始--- 一直想在leetcode上面刷题,但是java刚刚摸了一下门,所以迟迟没有动手,今天做了第一道题,感觉自己实在菜的不行,但是还是学到了很多东西, 就记录一下遇到的问题. 首先 ...
- 【leetcode算法-简单】7.整数反转
[题目描述] 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123输出: 321 示例 2: 输入: -123输出: -321 示例 3: 输入: 12 ...
随机推荐
- ios点击产生波纹效果
ios点击产生波纹效果 by 伍雪颖 - (void)viewDidLoad { [super viewDidLoad]; RippleView = [[UIView alloc] initWithF ...
- Swift - 使用xib添加新界面
除了使用storyboard外,我们还可以使用xib来设计并创建页面. 1,下面通过一个样例来演示: (1)点击主界面的“信息”按钮,页面切换到信息界面 (2)点击信息界面的“返回”,关闭信息界面,回 ...
- 打开asp出现An error occurred on the server when processing the URL
分享到: 2013-01-21 15:38 提问者采纳 方法一 以管理员身份运行CMD,将目录定位到%windir%\system32\inetsrv\,然后执行appcmd set co ...
- VirtualBox,Kernel driver not installed (rc=-1908)
http://hi.baidu.com/spt_form/item/316d6207b47b8ee03499020a VirtualBox,Kernel driver not installed (r ...
- ThinkPhp学习08
原文:ThinkPhp学习08 一.普通查询方式 a.字符串 $arr=$m->where("sex=0 and username='gege'")->find(); ...
- hdu 4715
#include<stdio.h> #include<string.h> int prime[1100000],p[1000000],ans; void pri() { ...
- 使用VS插件在VS2012/2013上编辑和调试Quick-Cocos2d-x的Lua代码
vs 也能够做lua 开发,并进行代码调试 依照以下文档,调试没问题. 參考文档: 点击打开链接
- 专注UI——有用技术:模糊搜索
在如今的项目中.须要做模糊搜索,在曾经技术的基础上非常快得完毕了第一版.大家先看看第一版的效果,我们一会做评论: 0基础: 我们可能部分源代码(附件中会有所有源代码) <span style=& ...
- freemarker自己定义标签(二)
freemarker自己定义标签 1.自己定义标签 通过自己定义标签,写一个反复指定字符串 2.实现源代码 <html> <head> <meta http-equiv= ...
- linux LVS DR模式配置
拓扑图: 测试环境:CentOS 6.5 X86 64位 配置步骤: 1. 安装测试环境 [root@UCS-1 ~]# yum -y install httpd [root@UCS-1 ~]# c ...