48. Rotate Image ~
题目:
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
链接: http://leetcode.com/problems/rotate-image/
4/15/2017
4ms, 8%
1 public class Solution { 2 public void rotate(int[][] matrix) { 3 int length = matrix.length; 4 for (int i = 0; i < (length + 1) / 2; i++) { 5 for (int j = i; j < length - 1 - i; j++) { 6 int tmp = matrix[i][j]; 7 matrix[i][j] = matrix[length - 1 - j][i]; 8 matrix[length - 1 - j][i] = matrix[length - 1 - i][length - 1 - j]; 9 matrix[length - 1 - i][length - 1 - j] = matrix[j][length - 1 - i]; 10 matrix[j][length - 1 - i] = tmp; 11 } 12 } 13 return; 14 } 15 }
别人的答案,聪明人啊,先上下对称,再左右对称
https://discuss.leetcode.com/topic/6796/a-common-method-to-rotate-the-image
1 /* 2 * clockwise rotate 3 * first reverse up to down, then swap the symmetry 4 * 1 2 3 7 8 9 7 4 1 5 * 4 5 6 => 4 5 6 => 8 5 2 6 * 7 8 9 1 2 3 9 6 3 7 */ 8 void rotate(vector<vector<int> > &matrix) { 9 reverse(matrix.begin(), matrix.end()); 10 for (int i = 0; i < matrix.size(); ++i) { 11 for (int j = i + 1; j < matrix[i].size(); ++j) 12 swap(matrix[i][j], matrix[j][i]); 13 } 14 }
类似的,先中心对称,再左右对称
https://discuss.leetcode.com/topic/9744/ac-java-in-place-solution-with-explanation-easy-to-understand
1 public class Solution { 2 public void rotate(int[][] matrix) { 3 for(int i = 0; i<matrix.length; i++){ 4 for(int j = i; j<matrix[0].length; j++){ 5 int temp = 0; 6 temp = matrix[i][j]; 7 matrix[i][j] = matrix[j][i]; 8 matrix[j][i] = temp; 9 } 10 } 11 for(int i =0 ; i<matrix.length; i++){ 12 for(int j = 0; j<matrix.length/2; j++){ 13 int temp = 0; 14 temp = matrix[i][j]; 15 matrix[i][j] = matrix[i][matrix.length-1-j]; 16 matrix[i][matrix.length-1-j] = temp; 17 } 18 } 19 } 20 }
这位大哥对python太了解了,还是写下来,有些最基本的我看来也不了解
https://discuss.leetcode.com/topic/15295/seven-short-solutions-1-to-7-lines
A[::-1]是把Python上下反转,这个没问题,zip()
in conjunction with the *
operator can be used to unzip a list.
举例说,是否inplace不讨论,不过最后是list of tuple,也有点怀疑啊
A = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]]A[::-1] = [[13, 14, 15, 16], [9, 10, 11, 12], [5, 6, 7, 8], [1, 2, 3, 4]]zip(*A[::-1])等于是把4个list进行unzip,得[(13, 9, 5, 1), (14, 10, 6, 2), (15, 11, 7, 3), (16, 12, 8, 4)]
class Solution:def rotate(self, A):A[:] = zip(*A[::-1])
发现这位大哥也认识到了这点,这个算法解决了这个问题, map等于将A当中每个tuple改成了list
map
(function, iterable, ...)
class Solution:def rotate(self, A):A[:] = map(list, zip(*A[::-1]))
更多讨论:
https://discuss.leetcode.com/category/56/rotate-image
转载于:https://www.cnblogs.com/panini/p/6718064.html
48. Rotate Image ~相关推荐
- 48. Rotate Image
48. Rotate Image 题目 You are given an n x n 2D matrix representing an image.Rotate the image by 90 de ...
- 【10】48. Rotate Image
48. Rotate Image Total Accepted: 96625 Total Submissions: 259249 Difficulty: Medium Contributors: Ad ...
- LeetCode - 48. Rotate Image
48. Rotate Image Problem's Link -------------------------------------------------------------------- ...
- 《每日一题》48. Rotate Image 旋转图像
给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...
- 【LeetCode】48. Rotate Image (2 solutions)
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- 48. Rotate Image java solutions
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- Leetcode 48 Rotate Image
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- 继续过中等难度.0309
. 8 String to Integer (atoi) 13.9% Medium . 151 Reverse Words in a String 15.7% Mediu ...
- 洛谷P3960 列队(Splay)
传送门 感觉自己好久不打数据结构已经完全不会了orz-- 据说正解树状数组?然而并不会 首先考虑一下每一次操作,就是把一个人从这一行中取出并放到行的最后,再从最后一列取出放到列的最后 那么这两种操作其 ...
最新文章
- compileReleaseJavaWithJavac
- jmeter的基本功能使用详解
- sentry 命令_sentry(二)集成sourcemap
- 如何在SAP云平台Neo环境里进行workflow(工作流)的开发
- 联邦快递“误运”华为包裹遭调查 联邦快递:全力配合
- Maven : error: missing or invalid dependency detected while loading class file 'RDD.class'
- 基于JAVA+SpringMVC+Mybatis+MYSQL的图书租赁系统
- Linux Repositories 2
- 在设计数据库时,使用代码,对数据项调整形成数字字典(小代码)
- 安卓地图的实现附源码
- iozone联机测试
- matlab去除图片水印_利用MATLAB去除图片中的水印
- 用计算机术语写毕业寄语,大学毕业寄语(精选50句)
- 【经典面试题】css如何画一个三角形?
- Web mfw Writeup
- 社群运营中品牌化和IP化运营实践
- 将word试卷匹配转换为结构化表格
- 目标检测---以制作yolov5的数据集为例,利用labelimg制作自己的深度学习目标检测数据集(正确方法)
- (附源码)ssm高校社团管理系统 毕业设计 234162
- 【leetcode】唯一摩尔斯密码词 c++ python
热门文章
- MathType公式保存后为什么字体会变化
- Android加载/处理超大图片神器!SubsamplingScaleImageView(subsampling-scale-image-view)【系列1】...
- SQL Server中的锁的简单学习
- angular学习笔记(二十五)-$http(3)-转换请求和响应格式
- request.getcontextPath() 详解
- 微软“SharePoint天天向上”第一期线上活动
- oracle expdp 多线程,Oracle expdp 过滤和并行
- 【Python学习系列十六】基于scikit-learn库逻辑回归训练模型(delta比赛代码)
- Java实现算法导论中线性规划单纯形算法
- 图像处理库OpenCV参考网址