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). ...
随机推荐
- linux基础教程---内容操作
一.寻找文件里的指定内容 寻找文件里的指定内容,输出内容所在行的所有信息 grep 被搜索内容 文件路径名 >grep var passwd //在 ...
- string类(四、字符串比较相关)
string类比较相关: 1. string.Compare [static] 1/ string.Compare(string A, string B); 比较两个string,返回整数表示二者在排 ...
- __file__
__file__ 是 python 的内置变量它的值等于文件名本身 [root@localhost ~]$ cat test.py #!/usr/bin/env python print(__file ...
- px像素单位与IOS像素单位的换算
本文转载至 http://blog.csdn.net/fanyuna/article/details/24032663 30px转成磅为单位=22磅=二号 磅=(像素/96)*72 =(30/96) ...
- 【BZOJ3416】Poi2013 Take-out 栈
[BZOJ3416]Poi2013 Take-out Description 小F喜欢玩一个消除游戏——take-out 保证k+1|n,保证输入数据有解这是一个单人游戏 游戏者的目标是消除初始时给定 ...
- Slave_SQL_Running: No mysql同步故障
参考:http://blog.itpub.net/29500582/viewspace-1318552/ http://blog.csdn.net/seteor/article/details/172 ...
- 基于开源博客系统(jpress)搭建网站
基于开源博客系统(jpress)搭建网站 JPress 使用 Java8 开发,基于流行的JFinal和Jboot框架. 目前JPress已经内置的文章和页面其实是两个模块,可以移除和新增其他模块,因 ...
- js 操作对象的引用和操作实际对象的区分
JavaScript高级程序设计-第3版-中 有这么一段话: 在操作对象时,实际上是在操作对象的引用而不是实际的对象.为此,引用类型的值是按引用访问的①. ① 这种说法不严密,当复制保存着对象的某个变 ...
- 160520、MyBatis的几种批量操作
MyBatis中批量插入 方法一: <insert id="insertbatch" parameterType="Java.util.List"> ...
- artTemplate模板使用补充
1. 添加辅助方法 ``template.helper(name, callback)``辅助方法一般用来进行字符串替换,如 UBB 替换.脏话替换等. 例如扩展一个UBB替换方法: template ...