LeetCode() Rotate Image

先改成对角线对换,然后再上下对换,如下所示

1,2, 3, 4

5, 6, 7, 8

9, 10,11,12

13,14,15,16

16,12, 8, 4

15,11, 7, 3

14,10, 6, 2

13, 9, 5, 1

13, 9, 5, 1

14,10, 6, 2

15,11, 7, 3

16,12, 8, 4

int n=a.size();
        int num=n-1;
        for(int i=0;i<n-1;++i)
        {
            for(int j=0;j<=num;++j)
            {
                swap(a[i][j],a[n-j-1][n-i-1]);
            }
            num--;
        }
        for(int i=0;i<n/2;++i)
			swap(a[i],a[n-i-1]);

  

时间: 2024-10-07 12:21:21

LeetCode() Rotate Image的相关文章

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 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],.....从这些中

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: 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

[LeetCode] Rotate Array / List

Question: http://leetcode.com/2010/04/rotating-array-in-place.html Rotate a one-dimensional array of n elements to the right by k steps.  For instance, with n=7 and k=3, the array {a, b, c, d, e, f, g} is rotated to {e, f, g, a, b, c, d} // O (n) pub

[leetcode]Rotate Image @ Python

原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: 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? 解题思路1:首先沿着副对角线翻转一次,然后沿着水平中线翻转一次,就可以得到所要求的矩阵了.时间复杂度O(n^2)

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 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. 原题链接:https://oj.leetcode.com/problems/rotate-list/ 得到链表长度,链表指向尾节点,

[LeetCode]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. 这道题是要求将单链表循环右移k次,每次移动一个结点. 考虑到k大于或等于链表的长度n,所以实际效果等同于循环右移k%n次. 遍历单链

[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 [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to solve this pro