Python 解leetcode:48. Rotate Image

  • 题目描述:把一个二维数组顺时针旋转90度;
  • 思路:
  1. 对于数组每一圈进行旋转,使用m控制圈数;
  2. 每一圈的四个元素顺时针替换,可以直接使用Python的解包,使用k控制每一圈的具体元素;
class Solution(object):
    def rotate(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        """
        n = len(matrix)
        m = 0
        while m <= n / 2:
            k = m
            while k < n - 1 - m:
                matrix[m][k], matrix[k][n-1-m], matrix[n-1-m][n-1-k], matrix[n-1-k][m] = \
                    matrix[n-1-k][m], matrix[m][k], matrix[k][n-1-m], matrix[n-1-m][n-1-k]
                k += 1
            m += 1
时间: 2024-07-30 09:04:38

Python 解leetcode:48. Rotate Image的相关文章

LeetCode 48. 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? 题目标签:Array 这道题目给了我们一个n * n的矩阵,让我们旋转图片.题目要求in-place,所以就不能用额外的空间了.一开始自己写了一个很原始的方法,结果虽然通过,但是速度太慢.只好去看看别人的方法,看完觉得,自己

Leetcode 48 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? 思路:把矩阵看成是由多个正方形一层一层包围起来的.转动的时候就是转动正方形的边,只要把边上的每个数依次替换就好,四个方向也就是进行四次替换,依次对每一层进行替换. 总层数 = matrix.length/2,第i层从第mat

LeetCode 48. Rotate Image My Submissions Question (矩阵旋转)

题目大意:给一个矩阵,将其按顺时针旋转90°. 题目分析:通法是先将矩阵转置,然后再反转每一行,或者是先反转每一列,然后再将其转置.I just want to say"It's amazing!".(forgivig my poor English!) 代码如下(代码怎么写已经不重要了!): class Solution { public: void rotate(vector<vector<int>>& matrix) { int r=matrix.

leetCode 48.Rotate Image (旋转图像) 解题思路和方法

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? 思路:其实就是旋转数组,没有什么难度,代码如下: public class Solution { public void rotate(int[][] matrix) { int[][] a =

Python解Leetcode: 226. Invert Binary Tree

leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): d

Python解Leetcode: 539. Minimum Time Difference

题目描述:给定一个由时间字符组成的列表,找出任意两个时间之间最小的差值. 思路: 把给定的链表排序,并且在排序的同时把60进制的时间转化成十进制整数: 遍历排序的数组,求出两个相邻值之间的差值: 求出首尾两个值之间的差值. class Solution(object): def findMinDifference(self, timePoints): """ :type timePoints: List[str] :rtype: int """

LeetCode 48. Rotate Image (C++)

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

19.2.7 [LeetCode 48] Rotate Image

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

Python 解leetcode:3. Longest Substring Without Repeating Characters

题目描述:求一个字符串的不含重复字符的最长连续子串的长度: 思路: 使用一个哈希表保存字符出现的位置: 使用left和right分别表示子串的最左和最右字符的下标: 遍历字符串,如果当前字符在哈希表中并且当前字符在哈希中的位置大于left,表明left和right之间存在和right索引上相同的字符,此时把left置为当前值在哈希表中的值: 把当前索引值+1,表示是第几个字符,求出当前最大长度: 3-4步重复,直到获得遍历完成: 感觉这是个动态规划题,暂时没用动态规划分析,后续再说. class