[leetcode 48] rotate image
1 题目
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?
2 思路
使用辅助空间比较简单。
不使用辅助空间,想了半天,没想出来。。
看别人的代码,原来是要翻转两次。。
3 代码
public void rotate(int[][] matrix) {
// 使用多余空间的
int length = matrix.length;
Integer[][] m = new Integer[length][length];
for(int i=;i<length;i++){
for(int j = ;j<length;j++){
m[j][length-i-] = matrix[i][j];
}
}
for(int i=;i<length;i++){
for(int j = ;j<length;j++){
matrix[i][j] = m[i][j];
}
}
}
// 不使用多余空间
// 法1: exchange up and down matrix[i][j] = matrix[length-1-i][j],swap symmetry matrix[i][j] = matrix[j][i]
public void rotate1(int[][] matrix) {
int length = matrix.length;
for(int i=;i<length/;i++){
for(int j = ;j<length;j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[length--i][j];
matrix[length--i][j] = temp;
}
}
for(int i=;i<length;i++){
for(int j = ;j<i;j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
// 法2: transpose(matrix[i][j]=matrix[j][i], flip horizontally matrix[i][j] = matrix[i][length-1-j]
[leetcode 48] rotate image的更多相关文章
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
- [LeetCode] 48. Rotate Image 旋转图像
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 (clockwise). ...
- LeetCode 48 Rotate Image(2D图像旋转问题)
题目链接: https://leetcode.com/problems/rotate-image/?tab=Description Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...
- leetCode 48.Rotate Image (旋转图像) 解题思路和方法
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- [leetcode]48. Rotate Image旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- LeetCode 48. Rotate Image My Submissions Question (矩阵旋转)
题目大意:给一个矩阵,将其按顺时针旋转90°. 题目分析:通法是先将矩阵转置,然后再反转每一行,或者是先反转每一列,然后再将其转置.I just want to say"It's amazi ...
- LeetCode 48. Rotate Image (C++)
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- 【刷题笔记】LeetCode 48. Rotate Image
题意 原地顺时针翻转一个 n*n 的矩阵 图解 下面例子中用 5*5 矩阵做示例,如下图,我们要把该矩阵顺时针翻转90度,并且不能使用另外的矩阵空间来暂存数据,而是原地改变矩阵中数值. 我的想法是这样 ...
随机推荐
- SQLserver聚集表、堆和索引
SQL Server 表使用下列两种方法之一来组织其分区中的数据页: 聚集表是有聚集索引的表.数据行基于聚集索引键按顺序存储.聚集索引按 B 树索引结构实现,B 树索引结构支持基于聚集索引键值对行进行 ...
- Asp.Net MVC4入门指南(9):查询详细信息和删除记录
在本教程中,您将查看自动生成的Details和Delete方法. 查询详细信息和删除记录 打开Movie控制器并查看Details方法. public ActionResult Details(int ...
- python logging模块可能会令人困惑的地方
python logging模块主要是python提供的通用日志系统,使用的方法其实挺简单的,这块就不多介绍.下面主要会讲到在使用python logging模块的时候,涉及到多个python文件的调 ...
- Getting Started With Hazelcast 读书笔记(第八章-第十章)
第八章到第十章就是一些介绍性的描述,吹的就是Hazelcast能使用在各种地方.. 第八章 -从外面看 1.Hazelcast做了一个memcache的java实现,方便py和php使用. 2.可 ...
- c#遍历目录及子目录下某类11型的所有的文件
DirectoryInfo directory = new DirectoryInfo("D:\\aa\\"); FileInfo[] files = directory.GetF ...
- jsp jsp指令
JSP 由HTML和java语句拼接而成的文本,后缀名为.jsp 1.Jsp翻译成servlet:先翻译,Tomcat将翻译后的文件放置在安装目录下(所有JSP页面本质上就是Servlet程序) 2. ...
- vs2015 附加到进程找不到w3wp.exe
vs2015 附加到进程找不到w3wp.exe 解决办法: 浏览器打开你访问的IIS地址后就出现了~!!!!!!!!!
- SQL:无法解决 equal to 操作的排序规则冲突。
更改存储过程的时候,在SQL中出现了 “无法解决 equal to 操作的排序规则冲突”错误,网上搜之,发现是表之间元素创建时排序规则不同(一个是collate Chinese_PRC_CI_AI_W ...
- SQl server 关于重复插入数据的测试
最近发布的脚本,有那种防止重复插入数据(包括存在时更新,不存在是插入的处理,判断的方向可能与下面的示例相反) 使用类似下面的 SQL declare @id int, @value int if no ...
- Linux的phpstudy mysql登录
使用绝对路径登录 /phpStudy/mysql/bin/mysql -uroot -p; 设置远程登录密码 GRANT ALL PRIVILEGES ON *.* TO 'itoffice'@'%' ...