1. Question

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]

]

2. Solution

这道题比较有技巧性。

  • 顺时针旋转,将矩阵按行,从上到下倒置,然后再把对称元素做交换。

  • 逆时针旋转,将矩阵按列,从左到右交换,然后再把对称元素做交换。

3. Code

顺时转旋转。

/*
* clockwise rotate
* first reverse up to down, then swap the symmetry
* 1 2 3 7 8 9 7 4 1
* 4 5 6 => 4 5 6 => 8 5 2
* 7 8 9 1 2 3 9 6 3
*/
void rotate(vector<vector<int> > &matrix) {
reverse(matrix.begin(), matrix.end());
for (int i = 0; i < matrix.size(); ++i) {
for (int j = i + 1; j < matrix[i].size(); ++j)
swap(matrix[i][j], matrix[j][i]);
}
}

逆时针旋转。

/*
* anticlockwise rotate
* first reverse left to right, then swap the symmetry
* 1 2 3 3 2 1 3 6 9
* 4 5 6 => 6 5 4 => 2 5 8
* 7 8 9 9 8 7 1 4 7
*/
void anti_rotate(vector<vector<int> > &matrix) {
for (auto vi : matrix) reverse(vi.begin(), vi.end());
for (int i = 0; i < matrix.size(); ++i) {
for (int j = i + 1; j < matrix[i].size(); ++j)
swap(matrix[i][j], matrix[j][i]);
}
}

LeetCode——Rotate Image的更多相关文章

  1. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  2. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  3. [LeetCode] Rotate List 旋转链表

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  4. [LeetCode] Rotate Image 旋转图像

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

  5. LeetCode——Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given  ...

  6. [LeetCode]Rotate Image(矩阵旋转)

    48. Rotate Image     Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...

  7. [leetcode]Rotate List @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-list/ 题意: Given a list, rotate the list to the right by ...

  8. [leetcode]Rotate Image @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...

  9. 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

    Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...

  10. [LeetCode] Rotate Function 旋转函数

    Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotati ...

随机推荐

  1. Redis单机主从高可用性优化

    版权声明:本文由陈龙原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/127 来源:腾云阁 https://www.qclou ...

  2. <a>标签实现链接和锚点的区别

    如果是实现链接,a标签中必须有href属性,并且属性值是合法的url 如果实现锚点,a标签中必须有name属性,当点击该标签时,会跳转到id同该标签的name值相同的元素处.

  3. C++ XML 序列化器

    http://www.cppblog.com/xlshcn/archive/2007/11/21/cppxmlserializer.html

  4. IDEA Tomcat部署时war和war exploded区别以及平时踩得坑

    war和war exploded的区别 在使用IDEA开发项目的时候,部署Tomcat的时候通常会出现下边的情况: 是选择war还是war exploded 这里首先看一下他们两个的区别: war模式 ...

  5. 170505、MySQL的or/in/union与索引优化

    假设订单业务表结构为: order(oid, date, uid, status, money, time, …) 其中: oid,订单ID,主键 date,下单日期,有普通索引,管理后台经常按照da ...

  6. CentOS7.4使用yum安装MySQL5.6

    CentOS默认数据库为mariadb可以使用yum安装MySQL5.6 系统版本查看 下载yum源安装 wget http://dev.mysql.com/get/mysql-community-r ...

  7. Oracle系统结构之修改oracle内存参数

    Linux主机16g内存,修改oracle数据库内存参数: 1.编辑/etc/fstab文件:针对tmpfs行将defaults改成defaults,size=12g(千万注意格式,不能出现错误) 修 ...

  8. 获取access_token示例代码

    文档中心--百度AI-百度AI开放平台 http://ai.baidu.com/docs#/NLP-API/top #include <iostream> #include <cur ...

  9. Intellij idea的Dependencies波浪线

    昨天在家做项目不知道搞了什么出现了大量波浪线.搞了大半天解决了下面的问题. 1.pom.xml出现波浪线.看右边 maven project-->Profiles 勾选dev 2.上面已勾选还有 ...

  10. github push error ---- recursion detected in die handler

    错误提示如下: 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 New Bitmap Image.bmp Coun ...