48. Rotate Image (matrix retation, transpose) Amazon problem
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的更多相关文章
- [leetcode 48] rotate image
1 题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwi ...
- [Leetcode][Python]48: Rotate Image
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 48: Rotate Imagehttps://leetcode.com/pr ...
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
- LeetCode 48 Rotate Image(2D图像旋转问题)
题目链接: https://leetcode.com/problems/rotate-image/?tab=Description Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...
- 刷题48. Rotate Image
一.题目说明 题目是48. Rotate Image,简而言之就是矩阵顺时针旋转90度.不允许使用额外的矩阵. 经过观察(写一个矩阵,多看几遍就知道了),旋转90度后: 第1行变为len-1列(最后一 ...
- 48. Rotate Image - LeetCode
Question 48. Rotate Image Solution 把这个二维数组(矩阵)看成一个一个环,循环每个环,循环每条边,每个边上的点进行旋转 public void rotate(int[ ...
- 48. Rotate Image
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- leetCode 48.Rotate Image (旋转图像) 解题思路和方法
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- LeetCode OJ 48. Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
随机推荐
- 云服务的三种形式laas,paas,saas
“云”其实是互联网的一个隐喻,“云计算”其实就是使用互联网来接入存储或者运行在远程服务器端的应用,数据,或者服务. 云也是分层的 任何一个在互联网上提供其服务的公司都可以叫做云计算公司.其实云计算分几 ...
- python作业1.1,编写登录模块
实现功能: 1.用户输入账户密码 2.验证账户是否存在于黑名单,如果存在于黑名单,则执行1,否则往下执行 3.验证用户名和密码. 3.1.如果验证成功,则打印欢迎信息并退出程序: 3.2.如果用户名存 ...
- Hsl PLC
https://github.com/dathlin/HslCommunication 地址
- TOJ 2711 Stars
描述 Astronomers often examine star maps where stars are represented by points on a plane and each sta ...
- 【python爬虫】用python编写LOL战绩查询
介绍一个简单的python爬虫,通过Tkinter创建一个客户端,当输入要查询的LOL用户名称的时候,可以显示出当前用户的所在服务器,当前战力和当前段位. 爬取网页地址:http://lol.duow ...
- bzoj 3243: [Noi2013]向量内积
Description 两个d 维向量A=[a1,a2,...,ad]与B=[b1,b2,...,bd]的内积为其相对应维度的权值的乘积和,即: 现有 n 个d 维向量x1,...,xn ,小喵喵想知 ...
- js录制视频并保存
使用webAPI录制视频 经测试, 只在谷歌和火狐浏览器里起效. 代码: const streamVideo = document.querySelector('.stream') const pla ...
- JUnit测试框架的使用
1.学习Junit框架的使用 可通过以下两个示例进行学习. A.Junit使用方法示例1 1)把Junit引入当前项目库中 新建一个 Java 工程—coolJUnit,打开项目coolJUnit 的 ...
- js【jquery】-事件
1.event对象 在IE.chrome中它是全局变量 与事件相关的信息会保存在event对象中,只有在事件发生的过程中,event才有信息 在其他浏览器中: 通过事件函数的第一个参数传入的 even ...
- linux安装lua_nginx_module模块
ngx_lua_module 是一个nginx http模块,它把 lua 解析器内嵌到 nginx,用来解析并执行lua 语言编写的网页后台脚本,可以用来实现灰度发布.另外淘宝的OpenResty也 ...