Rotate the image by 90 degrees (clockwise).

Given input matrix =
[
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]]
], rotate the input matrix in-place such that it becomes:
[
[9,5,1],
[10,6,2],
[11,7,3]
[12,8,4]
]

[[1,2,3,4],        [[9,10,11,12],                   [[9,5,1],
[5,6,7,8],  ==>          [5,6,7,8],         ==>          [10,6,2],
[9,10,11,12]]                 [1,2,3,4]]                          [11,7,3],
                      [12,8,4]]

 map(list, zip(*(matrix[::-1])))

Rotate the image by 90 degrees (anti-clockwise).

Given input matrix =
[
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]]
], rotate the input matrix in-place such that it becomes:
[
[4,8,12],
[3,7,11],
[2,6,10]
[1,5,9]
]

[[1,2,3,4],        [[1,5,9],                    [[4,8,12],
[5,6,7,8],  ==>          [2,6,10],         ==>          [3,7,11],
[9,10,11,12]]                  [3,7,11],                         [2,6,10],
            [4,8,12]]         [1,5,9]]

 map(list, zip(*matrix))[::-1]

如果matrix是n*n的, 然后需要in-place改的话.可以用以下的模板.

/*
* clockwise rotate
* first reverse up to down, then swap the symmetry
* 1 2 3 7 8 9 7 4 1
* 4 5 6 => 4 5 6 => 8 5 2
* 7 8 9 1 2 3 9 6 3
*/
      matrix[:] = matrix[::-1] # 记住要用matrix[:], 不然更改的不对
#matrix = matrix[::-1]
for i in range(len(matrix)):
for j in range(i+1, len(matrix)):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
/*
* anticlockwise rotate
* first reverse left to right, then swap the symmetry
* 1 2 3 3 2 1 3 6 9
* 4 5 6 => 6 5 4 => 2 5 8
* 7 8 9 9 8 7 1 4 7
*/
       matrix[:] = [each[::-1] for each in matrix]
3       for i in range(len(matrix)):
4 for j in range(i+1, len(matrix)):
5 matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

[Leetcode] Template to rotate matrix的更多相关文章

  1. Rotate Matrix by One

    记得有道Amazon的OA题目,好像是给定一个矩阵,让把矩阵的每个元素向右shift一个位置.这道题之前没有好好自己想过.今天正好刷到了rotate matrix,所以正好一块想了. 思路是类似Lee ...

  2. [Leetcode][Python]48: Rotate Image

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 48: Rotate Imagehttps://leetcode.com/pr ...

  3. 【一天一道LeetCode】#59. Spiral Matrix II

    一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

  4. Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)

    Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作) 题目描述 在MATLAB中,reshape是一个非常有用的函数,它可以将矩阵变为另一种形状且保持数据 ...

  5. 【LeetCode】61. Rotate List 解题报告(Python)

    [LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  6. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

  7. 【一天一道LeetCode】#48. Rotate Image

    一天一道LeetCode系列 (一)题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 ...

  8. 【LeetCode】48. Rotate Image

    Difficulty:medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/rotate-image/ ...

  9. 【LeetCode】48. Rotate Image 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. solus 系统 - 安装编译工具

    将会安装 gcc,make等工具 sudo eopkg install -c system.devel VirtualBox工具 https://solus-project.com/articles/ ...

  2. simulation vs emulation

    Hardware emulation, the use of special purpose hardware to emulate the behavior of a yet-to-be-built ...

  3. 看看大神们是怎么解决一些【bng】的哪!!!!

    作者:姚冬  遇到bng的分享 我曾经做了两年大型软件的维护工作,那个项目有10多年了,大约3000万行以上的代码,参与过开发的有数千人,代码checkout出来有大约5个GB,而且bug特别多,op ...

  4. Inotify+rsync实现实时数据同步

    使用rsync可以实现数据同步,但是即使使用crontab定时任务最小执行间隔为1分钟,在数据实时性要求比较高场合需使用inotify+rsync实现实时同步 下载inotify wget https ...

  5. db2 cpu使用率高问题分析处理

    性能调优步骤 明确问题->收集数据->分析数据->细化.定位问题->优化 环境: db2 问题:%usr CPU高,大约99%,db2sysc进程使用的最多 收集数据 ---系 ...

  6. {MySQL的逻辑查询语句的执行顺序}一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析

    MySQL的逻辑查询语句的执行顺序 阅读目录 一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析 一 SEL ...

  7. Python:time模块、calendar模块

    time模块 import time 获取时间戳 >>>time.time() #1532418950.7246091 获取时间元组 >>> time.localt ...

  8. qemu无界面启动,并重定向输出到终端

    qemu-system-x86_64  -kernel bzImage -initrd /mnt/rootfs.cpio.gz  /dev/zero -m 2G -nographic -append ...

  9. RHEL6.2的安装文档

    1 Installing RHEL 6.2 1.1 开始安装 选择“Install or upgrade an existing system”: 1.2 光盘检测 选择“Skip”跳过安装介质的检查 ...

  10. 洛谷P3158 放棋子 [CQOI2011] dp+数论

    正解:dp+数论 解题报告: 传送门! 考虑对每种颜色的棋子单独考虑鸭,那显然有,当某一行或某一列已经被占据的时候,那一行/一列就不能再放别的颜色的棋子了,相当于直接把那一行/一列直接消了 显然就能考 ...