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. Redis缓存相关问题总结

    使用缓存是系统性能优化的第一黄金法则. 缓存的设计和使用对一个系统的性能至关重要,平时接触到项目无论多少也都会在某些层面用到缓存,比如用HashMap实现,Ehcache,memcached.redi ...

  2. [转]Linux下Python与C++混合编程

    转自:http://www.cnblogs.com/tevic/p/3645197.html 最近在做一个CUDA的项目,记录下学习心得. 系统 Linux 3.11.0-19-generic #33 ...

  3. js鼠标拖动(转载)

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

  4. C#编程经验-VS Debug

    F11 OneStepDebugF10 ProcessDebugbreakPointDebug(quick location,then use one step debug)

  5. Ubuntu 14.10 下安装Ambari 问题汇总

    在编译安装Ambari时候遇到了很多问题,现在记录一下 1 got error npm ERR! phantomjs@1.9.12 install while building ambari-web ...

  6. docker容器内存占用 之 系统cache,docker下java的内存该如何配置

    缘起: 监控(docker stats)显示容器内存被用完了,进入容器瞅了瞅,没有发现使用内存多的进程,使用awk等工具把容器所有进程使用的内存加起来看看,距离用完还远了去了,何故? 分析: 该不会d ...

  7. 对pytorch中Tensor的剖析

    不是python层面Tensor的剖析,是C层面的剖析. 看pytorch下lib库中的TH好一阵子了,TH也是torch7下面的一个重要的库. 可以在torch的github上看到相关文档.看了半天 ...

  8. nginx的启动,停止和重启

    启动 启动代码格式:nginx安装目录地址 -c nginx配置文件地址 例如: [root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /us ...

  9. SQLServer、MySQL、Oracle如何查看所有表的条数

    SQLServer: create table #t(name varchar(255), rows bigint, reserved varchar(20), data varchar(20), i ...

  10. django 补充和中间件

    配置 from django.conf import settings form组件 from django.forms import Formfrom django.forms import fie ...