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的更多相关文章

  1. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  2. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  3. LeetCode 48. Rotate Image(旋转图像)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  4. [LeetCode] 48. Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  5. LeetCode 48 Rotate Image(2D图像旋转问题)

    题目链接: https://leetcode.com/problems/rotate-image/?tab=Description   Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...

  6. [leetcode 48] rotate image

    1 题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwi ...

  7. leetCode 48.Rotate Image (旋转图像) 解题思路和方法

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

  8. [array] leetcode - 54. Spiral Matrix - Medium

    leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...

  9. [leetcode]48. Rotate Image旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

随机推荐

  1. 内存泄漏监测-LeakCanary

    内存泄漏监测方法之使用LeakCanary LeakCanary出处: github:https://github.com/square/leakcanary/issues square 公司 这个公 ...

  2. 翻译连载 | 附录 A:Transducing(下)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇

    原文地址:Functional-Light-JS 原文作者:Kyle Simpson-<You-Dont-Know-JS>作者 关于译者:这是一个流淌着沪江血液的纯粹工程:认真,是 HTM ...

  3. 简单背包问题(0032)-swust oj

    简单背包问题(0032) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 5657 Accepted: 1714 Accepted 搜 ...

  4. 如何滚动更新 Service?- 每天5分钟玩转 Docker 容器技术(102)

    在前面的实验中,我们部署了多个副本的服务,本节将讨论如何滚动更新每一个副本. 滚动更新降低了应用更新的风险,如果某个副本更新失败,整个更新将暂停,其他副本则可以继续提供服务.同时,在更新的过程中,总是 ...

  5. VUE 与其他常见前端框架对比

    对比其他框架(转官方文档) 这个页面无疑是最难编写的,但我们认为它也是非常重要的.或许你曾遇到了一些问题并且已经用其他的框架解决了.你来这里的目的是看看 Vue 是否有更好的解决方案.这也是我们在此想 ...

  6. JavaSE初步学习笔记

    PS:个人用来随时记录学习的过程,格式比较混乱,仅供个人参考与复习知识点 Dos命令行,课程中常见的命令 Dir:列出当前目录下包含的文件 Md:在当前目录下创建文件 Rd:在当前目录下删除指定文件夹 ...

  7. PAT 1042. Shuffling Machine (20)

    1042. Shuffling Machine (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shu ...

  8. 通过YUM升级centOS内核,以便安装docker

    安装Docker要满足一定的条件,对于cents系统,要求必须是64位,并且内核版本是3.10以上. 如果你的centos操作系统内核低于3.10,需要升级到这个版本以上,才能安装docker. 第一 ...

  9. [译]在Asp.Net Core 中使用外部登陆(google、微博...)

    原文出自Rui Figueiredo的博文<External Login Providers in ASP.NET Core> 摘要:本文主要介绍了使用外部登陆提供程序登陆的流程,以及身份 ...

  10. Android数据绑定技二,企业级开发

    PS:上一篇文章写了Databinding的简单使用,写了一个绑定textview的示例,和绑定的一些用法,估计有的人会说,之前的写的好好的,为什么要数据绑定这样的写法呢,没办法,社会在进步,当然是怎 ...