题目

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?

代码:oj测试通过 Runtime: 53 ms

 class Solution:
# @param matrix, a list of lists of integers
# @return a list of lists of integers
def rotate(self, matrix):
if matrix is None:
return None
if len(matrix[0]) < 2 :
return matrix N = len(matrix[0]) for i in range(0, N/2, 1):
for j in range(i, N-i-1, 1):
ori_row = i
ori_col = j
row = ori_row
col = ori_col
for times in range(3):
new_row = col
new_col = N-row-1
matrix[ori_row][ori_col],matrix[new_row][new_col] = matrix[new_row][new_col],matrix[ori_row][ori_col]
row = new_row
col = new_col
return matrix

思路:

题意是将一个矩阵顺时针旋转90°

小白的解决方法是由外层向里层逐层旋转;每层能够组成正方形对角线的四个元素依次窜一个位置(a b c d 变成 d a b c)。

四个元素转换位置的时候用到一个数组操作的技巧,每次都要第一个位置的元素当成tmp,交换第一个位置的元素与指针所指元素的位置。

原始:a b c d

第一次交换:b a c d

第二次交换:c a b d

第三次交换:d a b c

这样的好处是代码简洁一些 思路比较连贯

Tips:

每层循环的边界条件一定要考虑清楚,小白一开始最外层循环的上届一直写成了N,导致一直不通过,实在是太低级的错误。以后还要加强代码的熟练度,避免出现这样的低级判断错误。

leetcode 【 Rotate Image 】python 实现的更多相关文章

  1. [leetcode]Rotate List @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-list/ 题意: Given a list, rotate the list to the right by ...

  2. [leetcode]Rotate Image @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...

  3. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  4. [LeetCode]题解(python):061-Rotate list

    题目来源 https://leetcode.com/problems/rotate-list/ Given a list, rotate the list to the right by k plac ...

  5. [LeetCode]题解(python):048-Rotate Image

    题目来源 https://leetcode.com/problems/rotate-image/ You are given an n x n 2D matrix representing an im ...

  6. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  7. [LeetCode] Rotate List 旋转链表

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  8. [LeetCode] Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  9. [LeetCode]题解(python):125 Valid Palindrome

    题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...

  10. [LeetCode]题解(python):120 Triangle

    题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...

随机推荐

  1. 配置Ubuntu DNS

    首先,你可以在/etc/hosts中加入一些主机名称和这些主机名称对应的IP地址,这是简单使用本机的静态查询.要访问Ubuntu DNS 服务器来进行查询,需要设置/etc/resolv.conf文件 ...

  2. [转]ubuntu16.04安装teamviewer12依赖包解决

    安装teamviewer下载地址:http://www.teamviewer.com/en/download/linux/ 下载的是:teamviewer_12.0.76279_i386.deb   ...

  3. Eucalyptus管理页面密码设置

    桉树环境什么的都已经是配置好了的,但是过了一段时间不用,也不知道密码是什么了,看着下面的页面也不知道如何进去,这里我们通过命令行的方式重置用户名和密码信息. 登陆clc所在机器,输入下命令: euar ...

  4. 优化Linux的内核参数来提高服务器并发处理能力

    提高Linux系统下的负载能力,可以使用nginx等原生并发处理能力就很强的web服务器 使用Apache的可以启用其Worker模式,来提高其并发处理能力 修改Linux的内核相关TCP参数,来最大 ...

  5. C#之MVC3继续整理问题

    1.注释验证[EmailAddress(ErrorMessage = "×")],用的MVC3框架,此处报错,找不到类“EmailAddress”,看到原文有using Syste ...

  6. IOS截取部分图片

    截取部分图片这么简单: - (void)loadView {     [[UIApplication sharedApplication] setStatusBarHidden:YES withAni ...

  7. cuda api查询问题

    在查询CUDA运行时API的时候,我用360极速浏览器的时候搜索结果一直不出来,但是用火狐的话就很流畅,所以建议大家在开发时还是用火狐浏览器.

  8. Bootstrap历练实例:popover插件中的方法

    方法 下面是一些弹出框(Popover)插件中有用的方法: 方法 描述 实例 Options: .popover(options) 向元素集合附加弹出框句柄. $().popover(options) ...

  9. C# StreamReader对象

    1.读取文件 输入流用于从外部源读取数据,在很多情况下,数据源可以是磁盘上的文件或网络的某些位置,任何可能发送数据的位置都可以是数据源,比如网络应用程序,web服务,甚至是控制台.StreamRead ...

  10. servlet层调用biz业务层出现浏览器 500错误,解决方法 dao数据访问层 数据库Util工具类都可能出错 通过新建一个测试类复制代码逐步测试查找出最终出错原因

    package com.swift.jztk.servlet; import java.io.IOException; import javax.servlet.ServletException; i ...