048 Rotate Image 旋转图像
给定一个 n × n 的二维矩阵表示一个图像。
将图像旋转 90 度(顺时针)。
注意:
你必须在原矩阵中旋转图像,请不要使用另一个矩阵来旋转图像。
例 1:
给出的输入矩阵 =
[
[1,2,3],
[4,5,6],
[7,8,9]
],
旋转输入矩阵,使其变为 :
[
[7,4,1],
[8,5,2],
[9,6,3]
]
例 2:
给出的输入矩阵 =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],
旋转输入矩阵,使其变为 :
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
详见:https://leetcode.com/problems/rotate-image/description/
Java实现:
class Solution {
public void rotate(int[][] matrix) {
int n=matrix.length;
//沿左上至右下对角线,交换对称对
for(int i=0;i<n;++i){
for(int j=i+1;j<n;++j){
int tmp=matrix[i][j];
matrix[i][j]=matrix[j][i];
matrix[j][i]=tmp;
}
}
//水平翻转每一行
for(int i=0;i<n;++i){
for(int j=0;j<n/2;++j){
int tmp=matrix[i][j];
matrix[i][j]=matrix[i][n-1-j];
matrix[i][n-1-j]=tmp;
}
}
}
}
参考:http://www.cnblogs.com/grandyang/p/4389572.html
https://www.cnblogs.com/lightwindy/p/8564385.html
048 Rotate Image 旋转图像的更多相关文章
- Leetcode48. Rotate Image旋转图像
给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...
- [LeetCode] Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- Java for LeetCode 048 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 (旋转图像) 解题思路和方法
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- [Leetcode][048] Rotate Image 略详细 (Java)
题目在这里 https://leetcode.com/problems/rotate-image/ [个人分析] 这个题目,我觉得就是考察基本功.考察细心的,算法方面没有太多东西,但是对于坐标的使用有 ...
- [leetcode]48. Rotate Image旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【LeetCode】048. Rotate Image
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- [LeetCode] 48. Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
随机推荐
- VC6.0实用小技巧
VC6.0的若干实用小技巧 .检测程序中的括号是否匹配 把光标移动到需要检测的括号(如大括号{}.方括号[].圆括号()和尖括号<>)前面,键入快捷键 “Ctrl+]”.如果括号匹配正确, ...
- bzoj2673
限制这么多 肯定是网络流 考虑连边 首先我们计算出每行最多放的棋子数$sx[i]$,每列最多放的棋子数$sy[i]$ 首先由源点向第$i$行连流量为$sx[i]$费用为$0$的边,第$i$列向汇点连流 ...
- p1020导弹拦截
传送门 P1020导弹拦截 题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度 ...
- bzoj 3714 [PA2014]Kuglarz——思路+最小生成树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3714 如果用s[ i ]表示前 i 个的奇偶性,那么c(i_j)表示s[ i-1 ]^s[ ...
- 云-资讯-Micron-Insight:云的形成方式 — 以及它的发展方向
ylbtech-云-资讯-Micron-Insight:云的形成方式 — 以及它的发展方向 1.返回顶部 1. 云的形成方式 — 以及它的发展方向 当你坐下来开始一天工作的时候,你可能不会考虑到你所做 ...
- java注解总结(1)
1.什么是注解 注解,主要提供一种机制,这种机制允许程序员在编写代码的同时可以直接编写元数据. 2.介绍 何为注解?--->元数据:描述数据自身的数据. 注解就是代码的元数据,他们包含了代码自身 ...
- node url
var url = require("url") url模块提供的三个方法: url.parse(urlStr[, parseQueryString][, slashesDenot ...
- 关于java基础中,接口里面父类的对象指向子类的引用
父类的引用指向子类的对象,它只能看的到父类的那些方法~ 子类自身的方法看不到-- ······························· 如: interface Singer { //定义了 ...
- Linux运行Tomcat下的war文件
1.查看Tomcat进程: ps -ef |grep tomcat 2.关闭Tomcat进程: kill -9 pid 3.关闭Tomcat运行: bin目录下执行 ./shutdown.sh 4. ...
- HTML5学习笔记(五)存储
HTML5 web 存储,一个比cookie更好的本地存储方式.数据以 键/值 对存在, web网页的数据只允许该网页访问使用.加的安全与快速.可以存储大量的数据,而不影响网站的性能. 客户端存储数据 ...