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. zabbix之 qq邮件报警

    一.安装sendmail或postfix[root@zabbix ~]# yum install sendmail –y                     #在线yum安装sendmail[ro ...

  2. ubuntu16.04下怎么关闭x server

    按住ctrl+alt+f1,进入命令行. 输入sudo /etc/init.d/lightdm stopsudo /etc/init.d/lightdm status 重启xserver?输入sudo ...

  3. docker4种网络最佳实战 --摘自https://www.cnblogs.com/iiiiher/p/8047114.html

    考: http://hicu.be/docker-container-network-types docker默认3中网络类型 参考: https://docs.docker.com/engine/u ...

  4. Net-Snmp工具(学习SNMP的工具,开源项目)简单使用

    https://blog.csdn.net/mrzhangzifu/article/details/77882371 Net-Snmp工具的安装与配置  操作系统:Ubuntu16.4  软件版本:n ...

  5. Pymysql部分

    安装: 1 执行SQL import pymysql # 创建连接 conn = pymysql.connect(host='172.30.2.233', port=3306, user='root' ...

  6. Python【每日一问】01

    问:深拷贝.浅拷贝.直接赋值的区别是什么?并举例说明 1.区别 (1)直接赋值:对象的引用 (2)浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象 (3)深拷贝(deepcopy): cop ...

  7. 云存储的未来:Scale Up还是Scale Out?

      云存储的几十年发展历程,其计算架构模型,也从Scale Up走向Scale Out.但是展望未来数字世界的海量需求,目前流行的模型还能够持续满足吗?本文通过对云存储历史的回顾,及对Scale Up ...

  8. npm安装与使用

    NPM 使用介绍 摘自:http://www.runoob.com/nodejs/nodejs-npm.html NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题, ...

  9. 编译CDH的spark1.5.2

    手动安装mvn大于3.3.3版本 下载解压,修改~/.bash_rc export MAVEN_HOME=/usr/local/apache-maven-3.3.9 export PATH=$MAVE ...

  10. Scrum Meeting 博客目录

    秘制牛肉团队 Scrum Meeting 博客汇总 一.Alpha阶段 团队第一次 # scrum meeting 团队第二次 # scrum meeting 团队第三次 # scrum meetin ...