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. AJAX返回总是ERROR或是没有数据的问题

    如果总是到ERROR,是因为async没有定义为false,设置为同步,数据类型要设置为text,不要用json. 示例: if (IDcard != "") { $.ajax({ ...

  2. el表达式用==和eq的注意事项

    eq和==一般是一样的,但是注意el表达式中使用==判断的时候不允许有空格,例如: ${job.jobName==requestScope.user.job.jobName?"selecte ...

  3. Maven Web项目部署到Tomcat下问题

    但是也遇到了很多问题,下面记录一下Web项目部署到Tomcat下的问题 1.普通的WEB项目,就是虽然是用maven搭建的,但是没有使用profiles.xml文件来配置参数.这样的项目可以通过以下的 ...

  4. c++ assert() 使用方法

    assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义: #include <assert.h> void assert( i ...

  5. sql的sp存储过程详解

    store procedure (存储过程) http://www.cnblogs.com/xiangzhong/p/5038338.html 调优的几个关键的步骤--sp_lock,sp_who h ...

  6. lumen之composer自动加载

    composer作为PHP的包管理工具,让PHP可以使用命名空间, 载入对应的类文件,不用理会文件引入的路劲问题,代码可读性也大大提高 composer 自动加载 composer 自动加载的规则 v ...

  7. 待选框、目标框select项目左右移动

    效果: 可以用一般的select multiple="multiple".自己写jq定义左移.右移.全部左移.全部右移.保存submit后端来操作select属性,方法参考html ...

  8. day5_判断价格输入是否是正整数或正小数

    def check_float_integer(s): #判断价格正确的正整数或正小数 s = str(s) if check_integer(s) == True: return True elif ...

  9. 关于.htaccess的设置

    RewriteEngine On #设置是否开始rewrite RewriteBase / #设置开始匹配的目录,比如web程序放在/var/www/html/test下,则这个值要设置为" ...

  10. oracle创建表空间 授权

    --创建表空间 临时表空间 create temporary tablespace xiaodai_temp tempfile '/main/app/oracle/oradata/devdb/xiao ...