题目

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. QT:轻松获取网页源码

    获取网页源码的小例子,代码很简单,就不多作解释了. 不过一定要注意网页的编码问题,否则会出现乱码的!!! #include <QtCore> #include <QtNetwork& ...

  2. 纯CSS美化的checkbox 和 radio

    html <!DOCTYPE HTML> <html> <head> <title>纯CSS3实现自定义美化复选框和单选框</title> ...

  3. 深入理解MFC子类化

    子类化,通俗来讲就是用自己的窗口处理函数来处理特定消息,并将自己其他消息还给标准(默认)窗口处理函数.在SDK中,通过SetWindowLong来指定一个自定义窗口处理函数:SetWindowLong ...

  4. Uber上海公司被司机打上门

    “Uber上周的工资没有到账,司机们都急了.”9月13日,<IT时报>记者接到Uber司机爆料,称Uber(优步)拖欠工资,客服给的解释是银行系统对接问题,但多名司机赶往Uber上海公司咨 ...

  5. android中控件公用产生的冲突的解决办法

    1.ViewPager嵌套HorizontalScrollView滑动冲突的解决办法,重写ViewPager public class ZdyViewPage extends ViewPager { ...

  6. javascript第十七课:this使用

    例如,我们要一个元素的值 function f1(){ alert(this.id); } document.getElementByid('#id').onclick=f1;  //将函数赋值给事件

  7. js添加、删除Cookie

    //cookie function addCookie(objName, objValue, objHours) { //添加cookie var str = objName + "=&qu ...

  8. 【UNIX网络编程(二)】基本TCP套接字编程函数

    基于TCP客户/server程序的套接字函数图例如以下: 运行网络I/O.一个进程必须做的第一件事就是调用socket函数.指定期望的通信协议类型. #include <sys/socket.h ...

  9. 最新首发Eclipse+CDT+android-ndk写纯c++安卓应用(附openGL Es)

    首先下载eclipse和cdt.我的版本依次是:Version: Indigo Service Release 2和Version: 1.0.0.201202111925,再下载windows的ndk ...

  10. Android的Bitmap和BitmapDrawable类解析-android学习之旅(六十)

    使用简单图片 使用Drawable对象 bitmap和BitmapDrawable对象 package peng.liu.test; import android.app.Activity; impo ...