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]
]

一个n x n的二维矩阵表示一个图像,将图像顺时针旋转90度。要求in-place,所以就不能用额外的空间了。

解法1: 先以对角线为轴翻转得到其转置矩阵,再以中间竖轴翻转。

1  2  3     1  4  7     7  4  1

4  5  6 -->   2  5  8  -->   8  5  2  

7  8  9       3  6  9      9  6  3

解法2: 先以反对角线翻转,在以中间水平轴翻转。

1  2  3     9  6  3    7  4  1

4  5  6 -->  8  5  2  -->   8  5  2  

7  8  9      7  4  1     9  6  3

解法一

class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
int n=matrix.size();
for(int i=;i<n;++i)
{
for(int j=;j<i;++j)
swap(matrix[i][j],matrix[j][i]);
}
for(int i=;i<n;++i)
for(int j=;j<n/;++j)
swap(matrix[i][j],matrix[i][n-j-]);
}
};

Rotate Image 旋转图像的更多相关文章

  1. 048 Rotate Image 旋转图像

    给定一个 n × n 的二维矩阵表示一个图像.将图像旋转 90 度(顺时针).注意:你必须在原矩阵中旋转图像,请不要使用另一个矩阵来旋转图像.例 1:给出的输入矩阵 = [  [1,2,3],  [4 ...

  2. Leetcode48. Rotate Image旋转图像

    给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...

  3. [LeetCode] 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 (旋转图像) 解题思路和方法

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

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

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

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

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

  7. [CareerCup] 1.6 Rotate Image 翻转图像

    1.6 Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a m ...

  8. [LeetCode] 61. Rotate List 旋转链表

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  9. LeetCode解题录-1~50

    [leetcode]1. Two Sum两数之和 Two Pointers, HashMap Easy [leetcode]2. Add Two Numbers两数相加 Math, LinkedLis ...

随机推荐

  1. python 全栈开发,Day77(图书管理系统)

    一.图书管理系统 完整代码链接: https://github.com/py3study/bms_multi 本项目使用session来实现一个简单的图书管理系统 未登录不允许访问后台: 直接访问后台 ...

  2. bootstrap 文本对齐风格

    Bootstrap通过定义四个类名来控制文本的对齐风格: ☑   .text-left:左对齐 ☑   .text-center:居中对齐 ☑   .text-right:右对齐 ☑   .text- ...

  3. ***php进行支付宝开发中return_url和notify_url的区别分析

    本文实例分析了php进行支付宝开发中return_url和notify_url的区别.分享给大家供大家参考.具体分析如下: 在支付宝处理业务中return_url,notify_url是返回些什么状态 ...

  4. Tomcat下指定JDK

  5. 对MariaDB10.0的Sphinx进行扩展

    已修改过的文件:http://pan.baidu.com/s/1o8DHvkA 将这两个文件放到MariaDB的解压目录后,再进行安装 /usr/local/mariadb-10.0.28/stora ...

  6. Mysql mysqld_safe启动与myslqd启动坑

    一.用mysqld_safe启动时候无法看到报错信息. 二.用mysqld启动时候可以看到日志实时打印.

  7. Docker 镜像的导入和导出

    镜像的导入和导出 export 和improt [root@#localhost docker]# docker run -ti ubuntu:update /bin/bash root@cbe3cb ...

  8. BZOJ1103 [POI2007]大都市meg dfs序 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1103 题意概括 一棵树上,一开始所有的边权值为1,我们要支持两种操作: 1. 修改某一条边的权值为 ...

  9. 005 Spark快速入门的简单程序案例

    参考:官网的quick start http://spark.apache.org/docs/1.6.0/quick-start.html 这里只是在shell命令行中简单的书写一些命令,做一个简单的 ...

  10. 069 Hue协作框架

    一:介绍 1.官网 官网:http://gethue.com/ 下载:http://archive.cloudera.com/cdh5/cdh/5/,只能在这里下载,不是Apache的 手册:http ...