题目

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. NFC-P2P MODE

    今日看见有关国内电信业者要合组TSM (Trusted Service Manager)提供NFC 服务的新闻, 这是属于NFC 所能提供的3种Mode中的Card emulation mode (就 ...

  2. C#线程间通讯

    using System;using System.Text;using System.Windows.Forms;using System.Threading; namespace 线程间通讯{   ...

  3. CentOS6无法本地登陆,ssh远程登陆没问题

    CentOS6无法本地登陆,ssh远程登陆没问题---使用CentOS自带的rsyslog分析调试 Apr 21 14:15:27 raccontroller init: tty (/dev/tty1 ...

  4. uva 10635 - Prince and Princess(LCS)

    题目连接:10635 - Prince and Princess 题目大意:给出n, m, k,求两个长度分别为m + 1 和 k + 1且由1~n * n组成的序列的最长公共子序列长的. 解题思路: ...

  5. C#—Dev XtraTabControl动态增加Tab和关闭选项卡方法

    C#—Dev XtraTabControl动态增加Tab和关闭选项卡方法,有需要的朋友可以参考下. 记录一下以免以后忘了 添加using DevExpress.XtraTab; 双击listview增 ...

  6. oracle归档日志管理

    归档日志(Archive Log)是非活动的重做日志备份.通过使用归档日志,可以保留所有重做历史记录,当数据库处于ARCHIVELOG模式并进行日志切换式,后台进程ARCH会将重做日志的内容保存到归档 ...

  7. oracle表空间创建及管理

    一.数据文件和数据库逻辑存储结构: 一个表空间包含一个或多个数据文件,一个表空间包含一个或多个段,一个段包含一个或多个区,一个区包含一个或多个连续的数据库块,一个数据库块包含一个或多个操作系统块.段是 ...

  8. odbc连接数据库

    using System; using System.Collections.Generic; using System.Text; using Console = System.Console; u ...

  9. Android系统环境变量配置

    ANDROID_HOME D:\Program Files\Android-sdk D:\AndroidSDK\android-sdk ANDROID_SDK_HOME %ANDROID_HOME% ...

  10. Spring学习之Ioc控制反转(2)

    开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...