LeetCode 48. Rotate Image

简介: 给定一个n x n的二维矩阵,以顺时针方向旋转90度

Description



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

Rotate the image by 90 degrees (clockwise).


Note:


You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.


Example 1:


Given input matrix =

[

[1,2,3],

[4,5,6],

[7,8,9]

],

rotate the input matrix in-place such that it becomes:

[

[7,4,1],

[8,5,2],

[9,6,3]

]


Example 2:


Given input matrix =

[

[ 5, 1, 9,11],

[ 2, 4, 8,10],

[13, 3, 6, 7],

[15,14,12,16]

],

rotate the input matrix in-place such that it becomes:

[

[15,13, 2, 5],

[14, 3, 4, 1],

[12, 6, 8, 9],

[16, 7,10,11]

]


描述



给定一个n x n的二维矩阵,以顺时针方向旋转90度


要求:

您必须就地旋转图像,这意味着您必须直接修改输入的2D矩阵, 不要分配另一个2D矩阵并进行旋转,即空间复杂度O(1)


思路



v2-a99166e82c1fb97243cfa332b194cdee_720w.jpg


  • 这道题主要考察发现规律,在不申请额外矩阵空间的条件下,我们需要知道如何转换,如下图,以四维矩阵进行说明
  • 如上图,需要观察发现:
  • 1-->4-->16-->13-->1
  • 2-->8-->15-->9-->2
  • 3-->12-->14-->5-->3
  • 如上是最外一层的变化,内层同样的道理
  • 每一行除去最后一个元素为一个循环,因为在此循环中:
  • 第一行的行标和最后一行的列标在循环过程中自身不变
  • 最后一行的列标和最后一行的行标在循环过程中自身不变
  • 最后一行的行标和第一列的列标在循环过程中自身不变
  • 第一列的列标和第一行的行标在循环过程中自身不变
  • 我们借助四个变量, rowtop, coleft, rowbot, colright,第一行行标,第一列列标,最后一行行标,最后一列列标
  • 于是我们保存第一行的一个元素,将第一列的对应元素给第一行,将最后一行的元素给第一列,将最后一列的元素给最后一行,将第一行的元素给最后一列
  • 如下


class Solution:
    def rotate(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        """
        # 正矩阵的维度
        rank = len(matrix[0])
        # 第一行,最左边的列,最后一行,最右边一列
        rowtop, coleft, rowbot, colright = 0, 0, rank-1, rank-1
        while rank > 1:
            for i in range(rank-1):
                # 第rowtop行的第coleft+i个元素保存
                temp = matrix[rowtop][coleft+i]
                # 将矩阵第一列的元素给矩阵第一行的元素
                matrix[rowtop][coleft+i] = matrix[rowbot-i][coleft]
                # 将矩阵最后一行的元素给矩阵第一列的元素
                matrix[rowbot-i][coleft] = matrix[rowbot][colright-i]
                # 将矩阵最后一列的元素给矩阵最后一行的元素
                matrix[rowbot][colright-i] = matrix[rowtop+i][colright]
                # 将矩阵第一行的元素给矩阵最后一列的元素
                matrix[rowtop+i][colright] = temp
            # 矩阵的行数(行列的个数相等,也可以表示为列数)减少2(上下各减少一行)
            rank -= 2
            # 最高的行数加一,表示行向内进一层,行数减少一行
            rowtop += 1
            # 最左边的列加一,表示向右进一层,列数减少一列
            coleft += 1
            # 最下边的行减一,表示向上进程一层,行数减少一行
            rowbot -= 1
            # 最右边的列减少一列,表示向左进一层,列数减少一列
            colright -= 1
        return


源代码文件在这里


目录
相关文章
LeetCode 189. Rotate Array
给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
65 0
LeetCode 189. Rotate Array
|
索引
LeetCode 61. Rotate List
给定链表,将列表向右旋转k个位置,其中k为非负数。
69 0
LeetCode 61. Rotate List
LeetCode 189. 旋转数组 Rotate Array
LeetCode 189. 旋转数组 Rotate Array
|
机器学习/深度学习 存储 人工智能
LeetCode 832. Flipping an Image
LeetCode 832. Flipping an Image
91 0
|
Python
Leetcode-Easy 796. Rotate String
Leetcode-Easy 796. Rotate String
58 0
Leetcode-Easy 796. Rotate String
LeetCode之Rotate Array
LeetCode之Rotate Array
113 0
LeetCode 61:旋转链表 Rotate List
​给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。 Given a linked list, rotate the list to the right by k places, where k is non-negative.
3088 0
|
算法 Java 索引
LeetCode 189:旋转数组 Rotate Array
公众号:爱写bug(ID:icodebugs) 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。 Given an array, rotate the array to the right by k steps, where k is non-negative.
751 0
|
C++
LeetCode 189 Rotate Array(旋转数组)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50600861 翻译 通过K步将一个有着n个元素的数组旋转到右侧。
766 0
|
12天前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
26 6