48. Rotate Image(旋转矩阵)
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Example 1:
Given input matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
], rotate the input matrix in-place such that it becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
Example 2:
Given input matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
], rotate the input matrix in-place such that it becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
] 先上下翻转,然后在对称翻转。
/*
* 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
*/
class Solution { public void rotate(int[][] matrix) {
int rows = matrix.length - 1;
int cols = matrix[0].length - 1;
for(int i = 0;i <=rows/2;i++)
for(int j = 0;j <= cols;j++ )
swap2(matrix,i,j,cols-i,j); for(int i = 0;i<=rows;i++)
for(int j =i+1;j<=cols;j++)
swap2(matrix, i, j,j,i);
} private void swap2(int[][] a,int i1,int j1,int i2,int j2) {
int temp = a[i1][j1];
a[i1][j1] = a[i2][j2];
a[i2][j2] = temp; } }
48. Rotate Image(旋转矩阵)的更多相关文章
- [Leetcode][Python]48: Rotate Image
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 48: Rotate Imagehttps://leetcode.com/pr ...
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
- 刷题48. Rotate Image
一.题目说明 题目是48. Rotate Image,简而言之就是矩阵顺时针旋转90度.不允许使用额外的矩阵. 经过观察(写一个矩阵,多看几遍就知道了),旋转90度后: 第1行变为len-1列(最后一 ...
- 48. Rotate Image - LeetCode
Question 48. Rotate Image Solution 把这个二维数组(矩阵)看成一个一个环,循环每个环,循环每条边,每个边上的点进行旋转 public void rotate(int[ ...
- [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
1 题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwi ...
- 48. 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 (旋转图像) 解题思路和方法
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- LeetCode OJ 48. Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
随机推荐
- hdu 1174:爆头(计算几何,三维叉积求点到线的距离)
爆头 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
- Python 读取json文件
创建json文件: { "fontFamily": "微软雅黑", "fontSize": 12, "BaseSettings&q ...
- uilabel 和uitextview 自适应大小
本文转载至 http://blog.csdn.net/liulichao20/article/details/8957752 分类: ios2013-05-21 22:06 321人阅读 评论(0) ...
- Java之自动拆装箱
顾名思义,自动拆装箱就是将基本类型和包装类进行自动的互相转换. JDK5.0后,将自动装箱/拆箱引Java中. 自动装箱的过程:每当需要一种类型的对象时,这种基本类型就自动地封装到与它相同类型的包装中 ...
- Layui前后台交互数据获取java
Layui简介 Layui是一款适用于后台程序员的UI框架,学习成本低.Json数据格式交互前后台,并且也相当适用单页面开发.有兴趣的朋友可以看看layui官网. Layui前后台数据交互 layui ...
- MemSQL start[c]up Round 1.b
二分查找题, 不知道用double的人,用LL果断错了... B. Stadium and Games time limit per test 1 second memory limit per te ...
- js让程序暂停运行的方法
//自己写的暂停毫秒数的函数 function pauseTime(millTime) { var start=Date.now(); while(true){ var nowTime=Date.no ...
- powerdesinger导出数据库说明文档
设置表结构要展示的属性,以及各个属性的展示列宽 不显示标题 右键单击items,选择format,然后Available栏中选择ListText选项卡,设置表格边框 保存为模板,Report-> ...
- debian卸载旧内核
debian卸载旧内核要先看看有哪些旧的内核,用命令: uname -a dpkg --get-selections |grep linux 如果你的内核是以kernel开头的就把上面的linux改成 ...
- Code Forces 645A Amity Assessment
A. Amity Assessment time limit per test2 seconds memory limit per test256 megabytes inputstandard in ...