• 题目描述:把一个二维数组顺时针旋转90度;

  • 思路:

  1. 对于数组每一圈进行旋转,使用m控制圈数;
  2. 每一圈的四个元素顺时针替换,可以直接使用Python的解包,使用k控制每一圈的具体元素;
class Solution(object):
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
n = len(matrix)
m = 0
while m <= n / 2:
k = m
while k < n - 1 - m:
matrix[m][k], matrix[k][n-1-m], matrix[n-1-m][n-1-k], matrix[n-1-k][m] = \
matrix[n-1-k][m], matrix[m][k], matrix[k][n-1-m], matrix[n-1-m][n-1-k]
k += 1
m += 1

Python 解leetcode:48. Rotate Image的更多相关文章

  1. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  2. [LeetCode] 48. Rotate Image 旋转图像

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

  3. LeetCode 48. Rotate Image(旋转图像)

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

  4. Python 解LeetCode:Intersection of Two Arrays

    最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...

  5. LeetCode 48 Rotate Image(2D图像旋转问题)

    题目链接: https://leetcode.com/problems/rotate-image/?tab=Description   Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...

  6. [leetcode 48] rotate image

    1 题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwi ...

  7. C#解leetcode 189. Rotate Array

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

  8. leetCode 48.Rotate Image (旋转图像) 解题思路和方法

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

  9. Python 解LeetCode:671. Second Minimum Node In a Binary Tree

    题目在这里,要求一个二叉树的倒数第二个小的值.二叉树的特点是父节点的值会小于子节点的值,父节点要么没有子节点,要不左右孩子节点都有. 分析一下,根据定义,跟节点的值肯定是二叉树中最小的值,剩下的只需要 ...

随机推荐

  1. SSH远程连接排错的过程

    SSH远程连接排错的过程 http://note.youdao.com/noteshare?id=bc81e9036cd2067cb1857ca9f54299a7 补充 ping命令用来测试主机之间网 ...

  2. 以8位并行数据为例确定crc-32的一般矩阵表示形式

    在进行数据校验时我们会使用到crc(循环冗余校验)校验的方式,例如在以太网通信网络中会对信息进行编码和校验,生成码采用的就是33位的 crc-32:x32+x26+x23+...+x2+x+1; (1 ...

  3. 元祖(tuple)

    元祖和列表几乎是一样的: 列表是可以进行修改的,它可以填加一个元素,也可以移除一个元素,但元祖是不能修改的 如果我们以后想导一个集合,这个集合以后不允许修改,我们用元祖:如何我们想让别人进行修改,我们 ...

  4. LOJ #6669 Nauuo and Binary Tree (交互题、树链剖分)

    题目链接 https://loj.ac/problem/6669 题解 Orz yyf太神了,出这种又有意思又有意义的好题造福人类-- 首先\(n\)次询问求出所有节点的深度. 考虑按深度扩展(BFS ...

  5. List的子类特点

    List的子类特点:  ArrayList:        底层数据结构是数组,查询快,增删慢        线程不安全,效率高  Vector:     底层数据结构是数组,查询快,增删慢     ...

  6. 【转】CAtlRegExp class .

    CAtlRegExp Class   CAtlRegExp 类用于表示并处理正则表达式.模板类,定义如下:     template < class CharTraits = CAtlRECha ...

  7. Flume-自定义 Source 读取 MySQL 数据

    开源实现:https://github.com/keedio/flume-ng-sql-source 这里记录的是自己手动实现. 测试中要读取的表 CREATE TABLE `student` ( ` ...

  8. win10设置开机开启数字锁定

    windows10开机小键盘默认关闭,网上查询修改InitialKeyboardIndicators键值为2,或者80000002,经过实际测试,均无效,键值8000000002有效,是中间8个0,开 ...

  9. 北大ACM - POJ试题分类

    1.入门水题 可用于练手与增强自信 POJ-1003POJ-1004 POJ-1005 POJ-1207 POJ-3299 POJ-2159 POJ-1083POJ-3094 2.初级 2.1. 基本 ...

  10. 01背包---P2392 kkksc03考前临时抱佛脚

    P2392 kkksc03考前临时抱佛脚 题解 01背包,类似于这道题,相似度99.999999%: 01-背包 P2663 越越的组队   一共有4科,每科的时间独立,然后每一科做一遍 P2663越 ...