[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 ...
随机推荐
- C++编程相关工具
1 文档类 (1) Doxygen 参考站点:http://www.doxygen.org Doxygen是一种适合C风格语言(如C++.C.IDL.Java甚至包括C#和PHP)的.开放源码的 ...
- 如何把py文件打包成exe可执行文件
如何把py文件打包成exe可执行文件 1.安装 pip install pyinstaller 或者 pip install -i https://pypi.douban.com/simple pyi ...
- MySQL使用mysqldump备份及还原
MySQL可以使用mysqldump进行数据的逻辑备份,配合开启bin log日志可以实现数据的全量恢复及增量恢复 MySQL版本查看 修改配置文件记录bin log日志 [mysqld] #bin ...
- 洛谷 P1208混合牛奶【贪心】
题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是 ...
- 简单示例用例(Simple Example Use Cases)--hive GettingStarted用例翻译
1.MovieLens User Ratings First, create a table with tab-delimited text file format: 首先,创建一个通过tab分隔的表 ...
- nginx url问题
测试需求,url自动加/后,发现重定向不带端口,即为 test.a.com:3443/abc nginx自动重定向301到 test.a.com/abc,端口消失. 分析可能和nginx默认配置se ...
- Altium Designer 绘图流程及快捷键
1.Shift+Ctrl+g 设置栅格捕捉大小 2.Q 切换单位 3.E+N +点击字体 改变字体大小 4.自动布线前需在Mechanical 层和keepout层添加一个边框 5.打过孔实现双面走线 ...
- 最全的MonkeyRunner自动化测试从入门到精通(1)
一.环境变量的配置 1.JDK环境变量的配置 步骤一:在官网上面下载jdk,JDK官网网址: http://www.oracle.com/technetwork/java/javase/downloa ...
- Win10系统桌面右键新建没有内容怎么恢复
Win10系统桌面右键新建没有内容怎么恢复 添加txt文本文档 运行->regedit,打开注册表编辑器 展开HKEY_CLASSES_ROOT 找到.txt 选中.txt,修改右侧窗格的“默认 ...
- P4827 [国家集训队] Crash 的文明世界
传送门:洛谷 题目大意:设$$S(i)=\sum_{j=1}^ndis(i,j)^k$$,求$S(1),S(2),\ldots,S(n)$. 数据范围:$n\leq 50000,k\leq 150$ ...