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). ...
随机推荐
- PIE SDK过滤控制
1. 功能简介 栅格数据前置过滤是在渲染之前对内存中的数据根据特定的规则进行处理,然后再进行数据渲染.本示例以定标为例进行示例代码编写. 定标(校准)是将遥感器所得的测量值变换为绝对亮度或变换为与地 ...
- MySQL 小抄
1. 登录 mysql - u root -pEnter Password: 2. 查询端口 mysql> show global variables like "port" ...
- 红米note_维修_开机键
1. 2.在线人工客服(20180919) 很荣幸为您服务,有什么问题可以帮助到您的- 我的手机 后边的 开机键 貌似 不太行了 您好,您是哪款手机 就是 要按 好几次 很用力 才能 开亮手机屏幕木 ...
- redis——基础知识
redis默认端口:6379 一.为何要用redis? redis广义上来讲类似于mongodb,rabitmq,都属于nosql——即非关系型数据库中的一种,通常而言,mongodb不能说是mq(消 ...
- 【3dsMax安装失败,如何卸载、安装3dMax 2010?】
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- git换行符问题
from: http://www.cnblogs.com/flying_bat/archive/2013/09/16/3324769.html 一.AutoCRLF#提交时转换为LF,检出时转换为CR ...
- MYSQL系列-MYSQL基础增强(Myql函数)
MYSQL基础增强(Myql函数) 在这里只介绍一些常用的,比较新颖的: 字符串函数: CONCAT://字符串连接函数 mysql> SELECT CONCAT('My', 'S', 'QL' ...
- java NIO之SelectedKey
SelectedKey是channel与Selector绑定的标记,每将一个channel注册到一个selector就会产生一个SelectedKey,并将这个SelectedKey放入到Select ...
- 九度oj题目1014:排名
题目1014:排名 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8130 解决:2417 题目描述: 今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排 ...
- Linux抓包工具:tcpdump
tcpdump 是一个命令行实用工具,允许你抓取和分析经过系统的流量数据包.它通常被用作于网络故障分析工具以及安全工具. tcpdump 是一款强大的工具,支持多种选项和过滤规则,适用场景十分广泛.由 ...