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]
]

题意:

In-place 顺时针旋转90度

Solution1: Try a simulation by matrix [2X2],  find the principle how to rotate

i.e:

Original:
1 2
4

Rotated:
1
4 2

Q:  How do we get value 3 from (1,0) to (0,0) ?

A:  flip matrix from up to down

Flip up down from original:

3
2

Q: How do we put values 1 and 4 to their own places?

A:  flip matrix by diagonal

Flip matrix by diagonal:

3 1
4 2

code

 /*
Time:O(n^2). We use nested 2 for loop.
Space: O(1). We only used constant extra space.
*/
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
// flip matrix from up to down
for(int i = 0; i < n/2; i++){
for(int j = 0; j< n; j++){
swap(matrix, i, j, n-i-1, j);
}
}
//flip matrix by diagonal
for(int i = 0; i < n; i++){
for(int j = i+1; j < n; j++){
swap(matrix, i, j, j, i);
}
}
}
private void swap(int[][] matrix, int i, int j, int a, int b){
int tmp = matrix[i][j];
matrix[i][j] = matrix[a][b];
matrix[a][b] = tmp;
}
}

[leetcode]48. Rotate Image旋转图像的更多相关文章

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

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

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

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

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

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

  4. LeetCode 48. Rotate Image(旋转图像)

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

  5. LeetCode 48 Rotate Image(2D图像旋转问题)

    题目链接: https://leetcode.com/problems/rotate-image/?tab=Description   Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...

  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. LeetCode 48. Rotate Image My Submissions Question (矩阵旋转)

    题目大意:给一个矩阵,将其按顺时针旋转90°. 题目分析:通法是先将矩阵转置,然后再反转每一行,或者是先反转每一列,然后再将其转置.I just want to say"It's amazi ...

  8. LeetCode 48. Rotate Image (C++)

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

  9. 【刷题笔记】LeetCode 48. Rotate Image

    题意 原地顺时针翻转一个 n*n 的矩阵 图解 下面例子中用 5*5 矩阵做示例,如下图,我们要把该矩阵顺时针翻转90度,并且不能使用另外的矩阵空间来暂存数据,而是原地改变矩阵中数值. 我的想法是这样 ...

随机推荐

  1. C# ZipHelper C#公共类 -- ZipArchive实现压缩和解压

    从网上找来个ZipArchive来压缩和解压缩的类,供参考吧 /******************************************************************** ...

  2. [转]SQL server2008 导入超大SQL脚本文件(超过10M)

    同事给我一个sqlserver的学习库,sql脚本导出有300m,gui执行有内存溢出的错误报出来,所以问了一下度娘,学而时习之:) 1. SQL server2008 导入超大SQL脚本文件(超过1 ...

  3. 深度图从ros数据类型转换成opencv数据类型

    摘要:ros下,利用realsense D435采集深度图,并将其转换成opencv的数据类型. 一. RGBD图像采集 通过image_transport包,根据给定的采集速度从realsense ...

  4. Tag (input) should be an empty-element tag.

    因为:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

  5. centos7 源码安装redis

    安装3.x [root@node1 ~]# yum install wget gcc-c++ make [root@node1 ~]# wget http://download.redis.io/re ...

  6. [转]IIS 日志记录时间和实际时间 不一样

    今天偶然发现 2003 系统IIS 日志记录时间和实际时间总是差了8个小时,也就是慢了8个小时.苦苦找了半天才发现如下办法能解决 ,特发来分享下 解决1:如果 IIS日志记录默认使用的是W3C扩展日志 ...

  7. Maven私服(Nexus)资源上传下载

    1.settings.xml (向私服上传资源需要) <!-- Snapshot包的管理/Releases包的管理/第三方包管理--> <server> <id>l ...

  8. 201772020113李清华《面向对象程序设计(java)》第一周学习总结

    201772020113<面向对象程序设计(java)>第一周学习总结 第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.com b ...

  9. Mysql 8.0修改密码

    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';

  10. PyCharm下的pywin32安装及使用

    转载http://www.mamicode.com/info-detail-2145088.html