题目

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:

Could you do this in-place?

原题链接(点我)

解题思路

顺时针方向旋转数组90°。这个题也是个没啥意思的题,自己画绘图,找找规律。就出来了。我举一个n=4的样例还说明下规律:

通过图能够看出A[0][0] = A[3][0],....。从这些中我们能够找到例如以下规律:

A[i][j] = A[n-1-j][i];

A[n-1-j][i] = A[n-1-i][n-1-j];

A[n-1-i][n-1-j] = A[j][n-1-i];

A[j][n-1-i] = A[i][j];(原来的A[i][j]).

规律出来了,代码也就好写了,只是的考虑边界的情况。这个自己还得举个n为奇数的样例看看。

代码实现

class Solution {
public:
void rotate(vector<vector<int> > &A) {
int m = A.size();
if(m<=0) return ;
int n = A[0].size();
for(int i=0; i<m/2;++i){
for(int j=i; j<n-1-i; ++j){
int temp = A[i][j];
A[i][j] = A[n-1-j][i];
A[n-1-j][i] = A[n-1-i][n-1-j];
A[n-1-i][n-1-j] = A[j][n-1-i];
A[j][n-1-i] = temp;
}
}
}
};
假设你认为本篇对你有收获,请帮顶。

另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
你能够搜索公众号:swalge 或者扫描下方二维码关注我

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/29810515
)

[LeetCode] Rotate Image [26]的更多相关文章

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

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

  2. [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 ...

  3. [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  ...

  4. [LeetCode] Rotate List 旋转链表

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

  5. [LeetCode] Rotate Image 旋转图像

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

  6. LeetCode——Rotate List

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

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

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

  8. [leetcode]Rotate List @ Python

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

  9. [leetcode]Rotate Image @ Python

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

随机推荐

  1. delphi 操作 TWebBrowser 实现自动填表(JQuery脚本与 OleVariant 方法)

    版本:DELPHI XE8 操作交通银行信用卡申请表单(2016-03-23),网址如下: https://creditcardapp.bankcomm.com/applynew/front/appl ...

  2. haproxy image跳转 haproxy匹配 匹配到了就停止,不会继续往下匹配

    <pre name="code" class="html">/***第一种 nginx 配置: location / { root /var/www ...

  3. css案例学习之float浮动

    代码: <!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

  4. KnockOutJS学习系列----(一)

    原文地址:http://www.cnblogs.com/n-pei/archive/2011/12/23/2299217.html 好几个月没去写博客了,最近也是因为项目紧张,不过这个不是借口,J. ...

  5. Web堡垒机

    最近研究了一下开源的web堡垒机,涉及两个,一个是gateone(python):一个是Web SSH Proxy(java),简单的对后者进行了改造,使其在登录与linux系统交互的时候,不需要使用 ...

  6. XSD (xml Schema Definition)

    .xsd文件是定义DataSet的XML文件,利用XML文件的结构优势容易可视化地设计DataSet,设计完它会生成相应的.cs文件,里面的内容就是对应的类型化的DataSet.你的代码里的DataA ...

  7. Erp第一章:初感

    Erp第一章:初感1.核心标志实现:内部集成.外部集成.内部集成包括实现产品研发.核心业务.数据采集:外部集成就是企业与供需链上的所有合作伙伴的共享信息集成.2.Erp难点在于打破传统企业四面墙,把流 ...

  8. js 创建类和继承的几种方法

    在面向对象编程中,类(class)是对象(object)的模板,定义了同一组对象(又称"实例")共有的属性和方法.JavaScript语言里是没有类的概念的,但是我们通过以下方法也 ...

  9. MSMQ是什么?

    MSMQ(MicroSoft Message Queue,微软消息队列)是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于相连的网络空间中的任一位 ...

  10. 安装rvm命令行工具

    rvm是一个命令行工具,可以提供一个便捷的多版本ruby环境的管理和切换. https://rvm.io/ 如果你打算学习ruby/rails, rvm是必不可少的工具之一. 这里所有的命令都是再用户 ...