[array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium
descrition
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]
]
解析
参见代码。小技巧:矩阵的对角线可以唯一确定一个矩阵。
code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution{
public:
void rotate(vector<vector<int> >& matrix){
//rotateNonInplace(matrix);
rotateNonInplace(matrix);
}
/*
(si,sj) ** (si,sj+k) *** (si,ej)
* *
* *
* (si+k,ej)
(ei-k,sj) *
* *
* *
(ei,sj)*** (ei,ej-k) ** (ei,ej)
*/
// time-O(n^2), space-O(1)
void rotateInplace(vector<vector<int> >& matrix){
int n = matrix.size();
int si = 0, sj = 0; // the top-left corner
int ei = n-1, ej = n-1; // the down-right corner
// (si, sj), (ei, ej)
while(si <= ei && sj<=ej){
for(int k=0; sj+k<=ej; k++){
int temp = matrix[si][sj+k];
matrix[si][sj+k] = matrix[ei-k][sj];
matrix[ei-k][sj] = matrix[ei][ej-k];
matrix[ei][ej-k] = matrix[si+k][ej];
matrix[si+k][ej] = temp;
}
si++;
sj++;
ei--;
ej--;
}
}
// time-O(n^2), space-O(n^2)
void rotateNonInplace(vector<vector<int> >& matrix){
int n = matrix.size();
vector<vector<int> > assit(n, vector<int>(n, 0));
// put the i-row in matrix to (n-1-i)-column in assit
for(int i=0; i<n; i++){
for(int k=0; k<n; k++){
assit[k][n-1-i] = matrix[i][k];
}
}
// copy assit to matrix
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
matrix[i][j] = assit[i][j];
}
}
}
};
int main()
{
return 0;
}
[array] leetcode - 48. Rotate Image - Medium的更多相关文章
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- 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 旋转图像
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 (旋转图像) 解题思路和方法
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- [array] leetcode - 54. Spiral Matrix - Medium
leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...
- [leetcode]48. Rotate Image旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
随机推荐
- 张高兴的 Xamarin.Forms 开发笔记:TapGestureRecognizer 的简单介绍与应用
最近很少写应用了,一直在忙关于 ASP.NET 的东西(哈欠...).抽点时间对 TapGestureRecognizer 做点总结. 一.简介 TapGestureRecognizer 就是对 Ta ...
- oracle 物化视图 job
在oracle数据库里边,创建物化视图之后,系统在DBMS_jobs文件夹下,会自动创建相应的job,右键执行job的时候报错如下: 问题:ORA-12012: 自动执行作业 198 出错 ORA-1 ...
- C++ 指针和引用 吐血整理 Pointer&Reference
说道C++的指针,很多人都很头疼,也很confuse.经常把它和变量名,引用(reference)等混淆,其实这最主要的原因是很多程序员对于基本知识的掌握有问题,从而导致的很多基本概念的混淆.本文就是 ...
- UITextField的使用小技巧
[tf setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];//修改placeHolder ...
- JAVA的向上转型和向下转型怎么理解呢?
在定义中是子类向父类转型称为向上转型,父类向子类转型是向下转型(必须先向上转型过,才能向下转型), 但是在下面类定义后,我得到的结果却不同.求大佬解惑 class superclass{ public ...
- POJ 1151 Wormholes spfa+反向建边+负环判断+链式前向星
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 49962 Accepted: 18421 Descr ...
- YiShop_商城网站设计应该注意什么
商城网站的建设,不是一件简单的事情,当然,也不要把商城网站的建设想得那么的复杂,在建设商城网站的时候 ,只要考虑到以下这些因素,定能把商城网站建设工作做好.那么商城网站建设要考虑哪些因素:(1)风格 ...
- <算法>进制转换超详细
16转10 用竖式计算: 16进制数的第0位的权值为16的0次方,第1位的权值为16的1次方,第2位的权值为16的2次方 第0位: 5 * 16^0 = 5 第1位: F * 16^1 = 240 第 ...
- Scala入门系列(十一):模式匹配
引言 模式匹配是Scala中非常有特色,非常强大的一种功能. 类似于Java中的switch case语法,但是模式匹配的功能要比它强大得多,switch只能对值进行匹配,但是Scala的模式匹配除了 ...
- windows 终端命令详解
打开"运行"对话框(Win+R),输入cmd,打开控制台命令窗口... 也可以通过cmd /c 命令 和 cmd /k 命令的方式来直接运行命令 注:/c表示执行完命令后关闭cmd ...