[LeetCode] Rotate Image [26]

题目

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],布布扣,bubuko.com

时间: 2025-01-08 02:12:25

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

Leetcode:Rotate List 链表旋转

Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. 解题分析: 不同于数组旋转,数组可以随机存取,数组的旋转可以巧妙的 分成两两部分递归旋转 对于链表的旋转,实际上

LeetCode: Rotate List [060]

[题目] Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. [题意] 给定一个链表L,和非负数K,要求把链表的后k个节点移到链表前 [思路] 先将指针指向指向倒数第K个节点,然后反转

LeetCode记录之26——Remove Duplicates from Sorted Array

国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等.拿起键盘干就行了.然后返回有序项的下标就可以. Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not

LeetCode: Rotate Image [047]

[题目] 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? [题意] 给定一个nXn的二维矩阵,按时钟方向旋转90度,不能使用额外的数据结构 [思路] 从外向内逐层旋转 [代码] class Solution { public: void rotateMatrix(vec

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 = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 思路: 我的思路:新建一个数组存放旋转后的内容,但是怎么把原数组的内容存放在数组中,不清楚. leetcode/discuss思路: 思路一:新建数组,复制原

[LeetCode] Rotate Function 旋转函数

Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a "rotation function" F on A as follow: F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1]. Ca

LeetCode Rotate Array 翻转数组

题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. 1 class Solution { 2 public: 3 void rotate(int nums[], int n, int k) { 4 if( !k || !n || n==1 || k==n ) return; 5 k %= n; 6 vector<int> cha; 7 cha.reserve

LeetCode: Rotate List 解题报告

Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. SOLUTION 1: 重点就是,要先变成循环链表,end.next = head;再执行ListNode he

LeetCode: Rotate Image 解题报告

Rotate ImageYou 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? SOLUTION 1: 我们可以把它当作多个嵌套的环来处理,一环加一环.从外环处理到内环.为了方便处理各种下标,我们定义top, bottom, left, right来限制环的左右上下边界. 1.