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. 【js字符串当做数组来使用】浪费一晚【想出了3个解决方案】

    数据库的所有数据都打成字符串发到前端. 不必把它的类型也强制转成int这类的,页面负责字符串的展示 这样做可以修改页面的数据 response.setHeader("Content-type ...

  2. zombodb  query dsl

    zombodb query dsl 是为了简化es 查询的处理,同时可以兼容基本上所有的es 操作 一个简单的查询,查询任何字段包含cats 以及dogs 的 SELECT * FROM table ...

  3. 小程序通过background-image设置背景图片

    微信小程序通过background-image设置背景:只支持线上图片和base64图片,不支持本地图片:base64图片设置步骤如下: 1.在网站http://imgbase64.duoshiton ...

  4. zabbix之 自定义内存使用率监控报警

    配置zabbix当内存剩余不足15%的时候触发报警   zabbix默认的剩余内存报警:Average Lack of available memory on server {HOST.NAME}{T ...

  5. php最常见最经典的算法题

    1.一群猴子排成一圈,按1,2,…,n依次编号.然后从第1只开始数,数到第m只,把它踢出圈,从它后面再开始数,再数到第m只,在把它踢出去…,如此不停的进行下去,直到最后只剩下一只猴子为止,那只猴子就叫 ...

  6. freemarker语法介绍及其入门教程实例

    # freemarker语法介绍及其入门教程实例 # ## FreeMarker标签使用 #####一.FreeMarker模板文件主要有4个部分组成</br>####  1.文本,直接输 ...

  7. Microsoft Azure News(7) Azure B系列虚拟机

    <Windows Azure Platform 系列文章目录> 最近微软Azure新数据中心上线了B系列的虚拟机,我这边研究了一下,给大家分享. Azure B系列虚拟机,其实是Burst ...

  8. springMVC配置文件web.xml与spring-servlet.xml与spring-jdbc.xml与logback.xml与redis.properties与pom.xml

    springMVC注解:@Controller @Service @Repository 分别标注于web层,service层,dao层. web.xml <?xml version=" ...

  9. LeetCode【88. 合并两个有序数组】

    首先想到的方法就是,假设一个nums3数组,然后,比较nums1与nums2的数值大小,然后,放在nums3中,再将nums3转移到nums1中. 实现起来很麻烦,1.没有考虑到下标问题,结果就Arr ...

  10. Nginx、HAProxy、LVS三者的优缺点

    一.Nginx优点: 1.工作在网络7层之上,可针对http应用做一些分流的策略,如针对域名.目录结构,它的正规规则比HAProxy更为强大和灵活,所以,目前为止广泛流行. 2.Nginx对网络稳定性 ...