leetcode 【 Rotate Image 】python 实现

题目

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?

代码:oj测试通过 Runtime: 53 ms

 1 class Solution:
 2     # @param matrix, a list of lists of integers
 3     # @return a list of lists of integers
 4     def rotate(self, matrix):
 5         if matrix is None:
 6             return None
 7         if len(matrix[0]) < 2 :
 8             return matrix
 9
10         N = len(matrix[0])
11
12         for i in range(0, N/2, 1):
13             for j in range(i, N-i-1, 1):
14                 ori_row = i
15                 ori_col = j
16                 row = ori_row
17                 col = ori_col
18                 for times in range(3):
19                     new_row = col
20                     new_col = N-row-1
21                     matrix[ori_row][ori_col],matrix[new_row][new_col] = matrix[new_row][new_col],matrix[ori_row][ori_col]
22                     row = new_row
23                     col = new_col
24         return matrix

思路:

题意是将一个矩阵顺时针旋转90°

小白的解决方法是由外层向里层逐层旋转;每层能够组成正方形对角线的四个元素依次窜一个位置(a b c d 变成 d a b c)。

四个元素转换位置的时候用到一个数组操作的技巧,每次都要第一个位置的元素当成tmp,交换第一个位置的元素与指针所指元素的位置。

原始:a b c d

第一次交换:b a c d

第二次交换:c a b d

第三次交换:d a b c

这样的好处是代码简洁一些 思路比较连贯

Tips:

每层循环的边界条件一定要考虑清楚,小白一开始最外层循环的上届一直写成了N,导致一直不通过,实在是太低级的错误。以后还要加强代码的熟练度,避免出现这样的低级判断错误。

时间: 2024-10-21 11:28:48

leetcode 【 Rotate Image 】python 实现的相关文章

[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)

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

[leetcode]Combination Sum @ Python

原题地址:https://oj.leetcode.com/problems/combination-sum/ 题意: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited

[leetcode]Interleaving String @ Python

原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = "aabcc",s2 = "dbbca", When s3 = "aadbbcbcac", return true.Whe

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

[leetcode]Sort List @ Python

原题地址:http://oj.leetcode.com/problems/sort-list/ 题意:链表的排序.要求:时间复杂度O(nlogn),空间复杂度O(1). 解题思路:由于题目对时间复杂度和空间复杂度要求比较高,所以查看了各种解法,最好的解法就是归并排序,由于链表在归并操作时并不需要像数组的归并操作那样分配一个临时数组空间,所以这样就是常数空间复杂度了,当然这里不考虑递归所产生的系统调用的栈.   这里涉及到一个链表常用的操作,即快慢指针的技巧.设置slow和fast指针,开始它们都

【LeetCode】【Python题解】Pascal&#39;s Triangle

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] 要求输入一个整数,返回一个表示杨辉三角的数组.我的方法是计算通项公式,首先是编写阶乘函数,然后计算C00,C10,C11即可 利用Python 的map嵌套可以很简洁地实现,核心代码只有一行! class So

[leetcode]Add Binary @ Python

原题地址:https://oj.leetcode.com/problems/add-binary/ 题意: Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 解题思路:提供两种实现方式吧. 代码一: class Solution: # @param a, a string # @pa

[leetcode]Gas Station @ Python

原题地址:https://oj.leetcode.com/problems/gas-station/ 题意: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to

[leetcode]Palindrome Partitioning @ Python

原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/ 题意: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab",Return [