[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). ...
随机推荐
- MyBatis开发学习记录
使用MyBatis时主要是完成POJO和SQL的映射规则 MyBatis基本构成: SqlSessionFactoryBuilder SqlSessionFactory SqlSession SqlM ...
- 第一章:关于Ehcache
PDF官方文档:http://www.ehcache.org/generated/2.10.4/pdf/About_Ehcache.pdf 1.什么是Ehcache? Ehcache是一种开源的基于标 ...
- redux中间件的原理——从懵逼到恍然大悟
前言react已经出来很久了,其生态圈之庞大,一锅炖不下!各种react-xx,已让我们不堪重负,github上随便一个demo,引入的模块至少都是五指之数+.看着头疼,嚼之无味…….在此建议新学者, ...
- JavaEE中的MVC(五)定制Struts——Action跳转JSP
在JavaEE中的MVC(三)中,我在Servlet中引入了命令模式的使用,采用Xml配置的方式,实现了一个Servlet调用多个不同的Action类,但是还不能实现页面地跳转,这一篇博客从之前的代码 ...
- VMWare安装Win10虚拟机
这两天突发奇想安了个win10虚拟机,在安装的过程中还遇到了不少麻烦,所以在此与大家分享下. 首先我们用VMWare12来安装,VMWare已经更新到14但是不太稳定,所以为了保险起见还是用12吧. ...
- IPhoneX网页布局
IPhoneX全面屏是十分科技化的,但是由于其圆角和摄像头刘海位置以及操控黑条的存在使得我们需要去对其样式做一些适配,没有X的同学可以开启 Xcode 9 的iPhone X 模拟器作为学习和调试. ...
- Thinkphp使用phpexcel导入文件并写入数据库
现实中,我们往往很多地方都需要这样批量导入数据,除了phpexcel还有csv等方法可以解决 下面详细介绍一下使用方法 首先在官方下载安装包解压到本地,然后复制保存到tp框架下的vendor目录下 h ...
- RBAC(Role-Based Access Control,基于角色的权限访问控制)—权限管理设计
RBAC模型的核心是在用户和权限之间引入了角色的概念,将用户和权限进行解耦,采用用户确定角色,角色分配权限,进而间接达到给用户分配角色的目的 这样采用的方式优点在于 (1)降低管理成本--由于一个角色 ...
- 配置scrapy-splash+python爬取医院信息(利用了scrapy-splash)
北京艾丽斯妇科医院(http://fuke.fuke120.com/) 首先先说一下配置splash 1.利用pip安装scrapy-splash库 pip install scrapy-splash ...
- sqlserver 查询所有表及记录行数
--查询所有表名 select name from sysobjects where xtype='u' select * from sys.tables --查询所有表名及对应架构 select t ...