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

 
 

There are 3 solutions.

1: use extra space O(n^2)

2. transpose + flip horizontally

class Solution {
void left2Right(int[][] matrix){
int n = matrix.length;
for(int i = 0; i<n; i++){//
for(int j = 0; j<(n/2); j++){//->
int temp = matrix[i][j];
matrix[i][j] = matrix[i][n-1-j];
matrix[i][n-1-j] = temp;
}
}
}
void up2Down(int[][] matrix){
int n = matrix.length;
for(int i = 0; i<n; i++){//->
for(int j = 0; j<(n/2); j++){//
int temp = matrix[j][i];
matrix[j][i] = matrix[n-1-j][i];
matrix[n-1-j][i] = temp;
}
}
}
void transpose(int[][] matrix){
int n = matrix.length;
for(int i = 0; i<n; i++){//->
for(int j = 0; j<i; j++){//
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
public void rotate(int[][] matrix) { //up2Down(matrix);
transpose(matrix);
left2Right(matrix);
}
}

3: just one operation (pattern among the in-place element)

reference: https://blog.csdn.net/happyaaaaaaaaaaa/article/details/51563752

class Solution {
public void rotate(int[][] matrix) {
//three solutions
int n = matrix.length;
for(int i = 0; i<=n/2; i++){//why n/2
for(int j = i; j<n-1-i; j++){//why i < j
int temp = matrix[i][j];
matrix[i][j] = matrix[n-1-j][i];
matrix[n-1-j][i] = matrix[n-1-i][n-1-j];
matrix[n-1-i][n-1-j] = matrix[j][n-1-i];
matrix[j][n-i-1] = temp;
} }
}
}

Caution: the boundary(pick the diagnosis)

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

 
 

48. Rotate Image (matrix retation, transpose) Amazon problem的更多相关文章

  1. [leetcode 48] rotate image

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

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

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

  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(2D图像旋转问题)

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

  5. 刷题48. Rotate Image

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

  6. 48. Rotate Image - LeetCode

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

  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. mysql - VARCHAR与VHAR的区别

    一, 基本介绍 char 和 varchar 两类型类似, 用来存储字符串,不同之处来自于他们的保存和检索的差别, char 属于固定的长度字符类型, 而varchar 属于可变长度的字符类型 值 C ...

  2. GreenPlum 大数据平台--非并行备份(六)

    一,非并行备份(pg_dump) 1) GP依然支持常规的PostgreSQL备份命令pg_dump和pg_dumpall 2) 备份将在Master主机上创建一个包含所有Segment数据的大的备份 ...

  3. 网页引用Font Awesome图标

    问题:最近在IIS上部署web项目的时候,发现浏览器总是报找不到woff.woff2字体的错误.导致浏览器加载字体报404错误,白白消耗了100-200毫秒的加载时间. 原因:因为服务器IIS不认SV ...

  4. CentOS初试

    由于实在是对ubuntu不太感冒,加上买的鸟哥又是拿CentOS做的例子,所以我就把ubuntu换成了CentOS6.5.依旧win7,CentOS 双系统,具体过程参照http://www.cnbl ...

  5. Docker的基本构架

    不多说,直接上干货! Docker的基本构架 Docker基于Client-Server架构,Docker daemon是服务端,Docker client是客户端. Docker的基本架构,如下图所 ...

  6. 《springcloud 四》服务保护机制

    服务保护机制SpringCloud Hystrix 微服务高可用技术 大型复杂的分布式系统中,高可用相关的技术架构非常重要. 高可用架构非常重要的一个环节,就是如何将分布式系统中的各个服务打造成高可用 ...

  7. java并发编程 - Exexctor简介

    Exexctor 常用类关系图 Executor 接口 Excutor 接口定义如下 ExecutorService ExecutorService 是一个比 Executor 使用更广泛的子类接口, ...

  8. 阅读redis源代码的一些体会

    最近在学习redis及阅读redis等程序的源码时,有一些收获,特记录到下面. 1.第一步,阅读源代码借助最好可以跟踪的工具去读,如sourceinsight. 我使用的是windows7环境,又因为 ...

  9. ASP.NET MVC 生命周期

    本文的目的旨在详细描述ASP.NET MVC请求从开始到结束的每一个过程.我希望能理解在浏览器输入URL并敲击回车来请求一个ASP.NET MVC网站的页面之后发生的任何事情. 为什么需要关心这些?有 ...

  10. FileUpload一键自动上传

    背景 源程序二次修改 传统的Asp.net WebForm开发 上传控件样式可自定义 分析 不能用第三方插件,因为源程序开发模式对异步的支持不友好而第三方插件大都是针对异步编程的 兼容IE8及以上和其 ...