[Leetcode] Template to rotate matrix
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的更多相关文章
- Rotate Matrix by One
记得有道Amazon的OA题目,好像是给定一个矩阵,让把矩阵的每个元素向右shift一个位置.这道题之前没有好好自己想过.今天正好刷到了rotate matrix,所以正好一块想了. 思路是类似Lee ...
- [Leetcode][Python]48: Rotate Image
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 48: Rotate Imagehttps://leetcode.com/pr ...
- 【一天一道LeetCode】#59. Spiral Matrix II
一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)
Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作) 题目描述 在MATLAB中,reshape是一个非常有用的函数,它可以将矩阵变为另一种形状且保持数据 ...
- 【LeetCode】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 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 ...
- 【一天一道LeetCode】#48. Rotate Image
一天一道LeetCode系列 (一)题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 ...
- 【LeetCode】48. Rotate Image
Difficulty:medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/rotate-image/ ...
- 【LeetCode】48. Rotate Image 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- Linux --Unbuntu php项目对应不同php版本
直入主题 因服务器上项目使用php版本有不同要求,特此解决一下. 下载 服务器上已有php7.0版本,安装7.0的方法自行百度,apt-get管理工具可直接install安装 然后安装一下需要的版本. ...
- Jenkins解决无法获取插件的办法(升级站点目录)
Jenkins解决无法获取插件的办法 可能是由于Jenkins的更新网站被QIANG,因此,请替换插件的服务器地址: http://mirror.xmission.com/jenkins/update ...
- 对iphone手机IMU的陀螺仪、加速度计、图像的时间戳做对齐处理
https://blog.csdn.net/chishuideyu/article/details/77479758 加速度计和陀螺仪的时间戳一致 ros 的message filters可以做时间同 ...
- iOS引用类型
强引用, 默认引用类型.被强引用指向的内存不被释放.强引用会对被引用对象的引用计数器+1,从而扩展对象的生命周期. 弱引用, 弱引用是医用特殊的引用类型.它不会增加引用计数器,因而不会扩展对象的生命周 ...
- SQL Server 2008 事件探查器(SQL SERVER Profiler)
要想很好地优化ERP系统,可以从客户端.服务器.网络等入手,对于我们M1系统的优化来说,SQL 语句的优化就起到很重要的作用了.为此,我们展开,学习了SQL SERVER 2008的事件探查器(SQL ...
- Concurrent Execution
Concurrent Execution — Python 3.7.2 documentation https://docs.python.org/3/library/concurrency.html
- [development][lockless][dpdk] 无锁队列
dpdk: http://dpdk.org/doc/guides/prog_guide/ring_lib.html#ring-library linux: https://lwn.net/Articl ...
- WinAPI Hook
1.抢先load 需要hook的dll,替换需要hook的函数的地址, 2.调用堆栈信息的获取: 3.内存信息的统计: 4.如何统计已经free掉的内存? 5.如何批量注入程序load的dll? IA ...
- 转:Java中子类是否可以继承父类的static变量和方法而呈现多态特性
原文地址:Java中子类是否可以继承父类的static变量和方法而呈现多态特性 静态方法 通常,在一个类中定义一个方法为static,那就是说,无需本类的对象即可调用此方法,关于static方法,声明 ...
- rem设置
html{ font-size:10vw; }