[leetcode]48. Rotate Image旋转图像
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旋转图像的更多相关文章
- [LeetCode] 48. Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- leetCode 48.Rotate Image (旋转图像) 解题思路和方法
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- [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(旋转图像)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- LeetCode 48 Rotate Image(2D图像旋转问题)
题目链接: https://leetcode.com/problems/rotate-image/?tab=Description Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...
- [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 48. Rotate Image My Submissions Question (矩阵旋转)
题目大意:给一个矩阵,将其按顺时针旋转90°. 题目分析:通法是先将矩阵转置,然后再反转每一行,或者是先反转每一列,然后再将其转置.I just want to say"It's amazi ...
- LeetCode 48. Rotate Image (C++)
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- 【刷题笔记】LeetCode 48. Rotate Image
题意 原地顺时针翻转一个 n*n 的矩阵 图解 下面例子中用 5*5 矩阵做示例,如下图,我们要把该矩阵顺时针翻转90度,并且不能使用另外的矩阵空间来暂存数据,而是原地改变矩阵中数值. 我的想法是这样 ...
随机推荐
- PythonStudy——算术运算符 Arithmetic operator
# 减法 # 加法 print(10 + 20) print('abc' + 'def') print([1, 2, 3] + [4, 5, 6]) Output: 30 abcdef [1, 2 ...
- fromkeys()
#fromkeys() #说明:用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值 #案例 d=[1,2,3] dict={} dict=dict.fromkeys( ...
- bootstrap弹出模态框会给body加padding的解决方法
bootstrap弹出模态框会给body加padding,导致页面缩放的解决方法: 在页面或是css文件里加上($paddingSize为less变量,需要改成像素或是其他单位,如12px,1rem) ...
- 【C++】vector内存机制和性能分析
转自:https://blog.csdn.net/mfcing/article/details/8746256 一些好的公司校园招聘过程中(包括笔试.面试环节),经常会涉及到STL中vector的使用 ...
- python2的reload模块
在刚开始写python程序的时候,都会遇到一个很头疼的问题——编码错误,在之前的文章中也做了介绍: 由__future__中unicode_literals引起的错误来研究python中的编码问题 . ...
- 【常用命令】Linux相关命令
[[TOC]] iostat - 查看系统I/O状况 -k Display statistics in kilobytes per second -m Display statistics in me ...
- C# 自定义类动态追加属性
利用Dynamic,需要.net4.0以上的支持 var dg = rel.ResultDocuments.FirstOrDefault()["dg"].AsBsonArray.G ...
- 《Java程序设计》 第二周学习总结
20175334 <Java程序设计>第二周学习总结 教材学习内容总结 了解Java编程风格 认识Java基本数据类型与数组 掌握Java运算符.表达式和语句 教材学习中的问题和解决过程 ...
- FastCGI点滴
FastCGI是一种二进制协议,用于将交互式程序与Web服务器连接.它是早期通用网关接口(CGI)的变体.FastCGI的主要目标是减少与Web服务器和CGI程序之间的接口相关的开销,允许服务器每单位 ...
- 2th Dec 2018
北京的冬天越来越冷了,是那种钻进骨头里的冷.果,爸爸又走了.每次离开都格外的难受,这种感觉是加剧的,一次比一次强烈.走的时候,你一脸的不高兴,能感觉出来你的不开心,你勉强让爷爷从我怀里面接过去.3个半 ...