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. ligerUI---ListBox(列表框可移动)

    写在前面: 对于可移动的列表框,ligerui中也对其进行了封装,可以直接照着demo拿来用,不过那都是直接在页面上静态初始化的数据,那么如何从后台获取? 前面有了对ligerui的一些组件的使用经验 ...

  2. Less 原理

    Less 原理 Less 包含一套自定义的语法及一个解析器,用户根据这些语法定义自己的样式规则,这些规则最终会通过解析器,编译生成对应的 CSS 文件. Less 并没有裁剪 CSS 原有的特性,更不 ...

  3. UEditor1.4.3.3实现图片上传、删除功能

    1.下载ueditor1.4.3.3 UTF-8的版本 2.新建一个项目,在项目中添加UEditor,把下载好的插件都放在ueditor这个文件夹中,在进行一些基本的配置 3.在根目录下新建一个为in ...

  4. Servlet交互【重定向 与 请求分派】详解

    Servlet交互 在serlvet中,需要调用另外一个资源来对浏览器的请求进行响应,两种方式实现: 调用HttpServletResponse.sendRedirect 方法实现 重定向 调用Req ...

  5. (翻译)使用Api分析器与Windows兼容包来编写智能的跨平台.NET Core应用

    本文翻译自Scott Hanselman博客: https://www.hanselman.com/blog/WritingSmarterCrossplatformNETCoreAppsWithThe ...

  6. 51nod 1058 N的阶乘的长度 位数公式

    1058 N的阶乘的长度基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注输入N求N的阶乘的10进制表示的长度.例如6! = 720,长度为3.Input输入N( ...

  7. NOIP2012junior—P1—质因数分解

    NOIP2012junior-P1-质因数分解 时间: 1000ms / 空间: 131072KB [背景] NOIP2012[描述] 已知正整数n 是两个不同的质数的乘积,试求出较大的那个质数. [ ...

  8. java web学习笔记 servlet

    关于java web web.xml中一般配置的都是与servlet先关的可以配置servlet filter listener context-param用来配置web应用的启动参数,可用通过Ser ...

  9. Hibernate常见问题 No row with the given identifier exists问题的解决办法及解决

    (1)在学习Hibernate的时候遇到了这个问题"No row with the given identifier exists"在网上一搜看到非常多人也遇到过这个问题! 问题的 ...

  10. Python 项目实践一(外星人入侵)第二篇

    接着上次的继续学习. 一 创建一个设置类 每次给游戏添加新功能时,通常也将引入一些新设置.下面来编写一个名为settings的模块,其中包含一个名为Settings的类,用于将所有设置存储在一个地方, ...