【LeetCode with Python】 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?

题意分析:

顺时针90度转动一个二维矩阵,要求in-place的算法,也就不是不能另外开一个新的矩阵然后把原矩阵上的元素一个个对应复制过来,而要在原矩阵上进行操作。

算法分析:

目前能想到的有两种方法。

第一种比较直观,首先将原矩阵按照水平轴上下翻转,然后按照左上到右下的对角线再反转,则等价于90度顺时针转动。这个方法的缺点是要反转两次,优点是直观,不容易出错。

第二种方法每个元素只需要调整一次,但是转化公式会比较复杂。对于矩阵matrix[n][n]中的某一点matrix[x][y](0<=x, y<n),90度顺时针转动后到了matrix[y][n-x-1],而matrix[y][n-x-1]到了matrix[n-x-1][n-y-1],matrix[n-x-1][n-y-1]到了matrix[n-y-1][x],matrix[n-y-1][x]又到了matrix[x][y],可见这四个点正好实现了一次旋转互换。

本文采用第二种方法,空间复杂度O(1),时间复杂度O(n*n)。

防坑警报:

n=1时直接返回原矩阵即可。

class Solution:
    # @param matrix, a list of lists of integers
    # @return a list of lists of integers
    def rotate(self, matrix):
        n = len(matrix)
        if 1 == n:
            return matrix
        round = int(n / 2)
        for x in range(0, round):
            for y in range(x, n - x - 1):
                tmp = matrix[n - y - 1][x]
                matrix[n - y - 1][x] = matrix[n - x - 1][n - y - 1]
                matrix[n - x - 1][n - y - 1] = matrix[y][n - x - 1]
                matrix[y][n - x - 1] = matrix[x][y]
                matrix[x][y] = tmp
        return matrix
时间: 2024-08-07 04:23:33

【LeetCode with Python】 Rotate Image的相关文章

【Leetcode长征系列】Letter Combinations of a Phone Number

原题: Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae"

【Leetcode长征系列】Merge k Sorted Lists

原题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:两条两条地合并.时间复杂度为O(n),n为所有链表节点和. 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :

【leetcode 字符串处理】Compare Version Numbers

[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume

【Leetcode长征系列】Construct Binary Tree from Inorder and Postorder Traversal

原题: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路:和上一题一样,后续我们可以通过最后一个值得到根的值,同样可以通过定位根的值得到左右子树的子集,递归求解即可. 代码: /** * Definition for binary tree * struct Tre

【Leetcode长征系列】Single Number II

原题: Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 思路: 用一个32位的数组存每一位bit值之后.得到答案后每一位除

【Leetcode长征系列】Pow(x, n)

原题: Implement pow(x, n). 思路:递归计算pow. class Solution { public: double pow(double x, int n) { long long int mid = n/2; int d = n%2; if(n==0) return 1; if(n==1) return x; if(d==1) return pow(x, (n/2)+1) * pow(x, n/2); else return pow(x, n/2) * pow(x, n/

【Leetcode长征系列】Sqrt(x)

原题: Implement int sqrt(int x). Compute and return the square root of x. ==============================以下为引用==================================== 牛顿迭代法 为了方便理解,就先以本题为例: 计算x2 = n的解,令f(x)=x2-n,相当于求解f(x)=0的解,如左图所示. 首先取x0,如果x0不是解,做一个经过(x0,f(x0))这个点的切线,与x轴的交

【新手学Python】一、基础篇

由于以前处理数据用Matlab和C,最近要处理大量文本文件,用C写实在是太繁琐,鉴于Python的强大文本处理能力,以及其在Deep Learning上有着很大优势,本人打算从即日起学习Python,谨以此系列博客记录学习点滴.文中如有错误,还望大牛们指出! Section 1: 本文是第一篇,当然也是基础,有了编程基础的我们都知道,学习一门语言什么最重要?当然先搞清楚数据类型和数据结构,有了这些,你才能去谈面向对象,才能去设计程序. Python的数据类型比较简单:1.整数;2.长整数;3.浮

【Leetcode长征系列】Balanced Binary Tree

原题: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofevery node never differ by more than 1. 思路:递归判断左右子树是否为BST. 代码: /** * Def