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(旋转矩阵)的更多相关文章

  1. [Leetcode][Python]48: Rotate Image

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 48: Rotate Imagehttps://leetcode.com/pr ...

  2. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  3. 刷题48. Rotate Image

    一.题目说明 题目是48. Rotate Image,简而言之就是矩阵顺时针旋转90度.不允许使用额外的矩阵. 经过观察(写一个矩阵,多看几遍就知道了),旋转90度后: 第1行变为len-1列(最后一 ...

  4. 48. Rotate Image - LeetCode

    Question 48. Rotate Image Solution 把这个二维数组(矩阵)看成一个一个环,循环每个环,循环每条边,每个边上的点进行旋转 public void rotate(int[ ...

  5. [LeetCode] 48. Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  6. [leetcode 48] rotate image

    1 题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwi ...

  7. 48. Rotate Image

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  8. leetCode 48.Rotate Image (旋转图像) 解题思路和方法

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

  9. LeetCode OJ 48. Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

随机推荐

  1. Ubuntu 编译安装搭配LNMP 环境

    这里用Nginx1.2.0+mysql5.6.33+php5.6.2搭配安装环境 ---------------------------------------------Nginx BEGIN--- ...

  2. iOS开发之-- 设置启动图片

    一.添加启动图片 点击Assets.xcassets进入图片管理,右击,弹出"New Launch Image"或点下面的+号创建Launch Image: 如图,右侧的勾选可以让 ...

  3. 利用hugo生成静态站点

    动机 使用Markdown撰写博客,并以静态页面形式发布. 选择hugo 现在jekyll似乎更加流行,但是jekyll是基于Ruby的,在windows下安装很繁琐. 而hugo是用go写的,win ...

  4. 三、Gradle初级教程——Gradle除了签名打包还能配置jar包

    1.gradle概念 构建工具,Groovy,Java. 2.gradle配置jar包,和libs文件夹导入jar包的区别 到此,还是这种方法导入JAR包比较方便.每次更新JAR包,只需要修改版本号就 ...

  5. Linux安装好系统后优化

    author:headsen chen date: 2018-05-14 15:35:49 1.快速更改国内yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc ...

  6. .NET 4.0 中的契约式编程

    契约式编程不是一门崭新的编程方法论.C/C++ 时代早已有之.Microsoft 在 .NET 4.0 中正式引入契约式编程库.博主以为契约式编程是一种相当不错的编程思想,每一个开发人员都应该掌握.它 ...

  7. powerdesigner 修改name时code不修改

    o

  8. Python--进阶处理2

    # ===================第二章:字符串和文本====================== # -----------------使用多个界定符分割字符串--------------- ...

  9. XML 配置里的 Bean 自动装配

    在XML文件中,先看一下下面的代码: <bean id="student" class="com.jeremy.spring.beans.student" ...

  10. googlr 黄金法则 监控

    googlr   黄金法则  监控