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

我的解法非常简洁,就用了一行代码,就是将给定的matrix重新整合,生成了一个新的matrix返回,所以并不是in-place的解法,但优点是十分的简洁,赏心悦目!

class Solution:
    # @param matrix, a list of lists of integers
    # @return a list of lists of integers
    def rotate(self, matrix):
        return map(lambda x:map(lambda y:y.pop(0),matrix[::-1]),range(len(matrix)))

再分享一个看到的非常帅的解法,这个算法就是in-place的了

#include <algorithm>

class Solution {
public:
    void rotate(vector<vector<int> > &matrix)
    {
        reverse(matrix.begin(), matrix.end());
        for (unsigned i = 0; i < matrix.size(); ++i)
            for (unsigned j = i+1; j < matrix[i].size(); ++j)
                swap (matrix[i][j], matrix[j][i]);
    }
};

乍一看非常难懂,就感觉只是以矩阵的对角线为轴交换了两边的像素点。其实不是。非常关键的一步是reverse()函数,它将matrix翻转了。如果你在纸上写一个nxn的矩阵(反正我是写下来才明白的),reverse之后相当于是把纸翻转过来,而且不是沿长边翻转,而是沿短边翻转(就是不是翻书那种翻转,而是纵向从底部向上的那种翻转),这样翻转之后最底部一行自然就成了最顶部一行。

第二步就是swap对角线两侧的元素,这样又是一次翻转,这次翻转更复杂一些,是沿矩阵对角线的一次翻转(也可以说是沿纸的对角线)

这时你会发现其实这就是把纸顺时针旋转了90度。

时间: 2024-10-23 23:57:52

【LeetCode】【Python题解】Rotate Image的相关文章

【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 题解]: 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个元素移动到链表的头部. 这道题的本质就是寻找链表的

【LeetCode】【Python题解】Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). Ho

LeetCode: palindromes 题解

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

LeetCode: LetterCombinations 题解

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", &q

LeetCode: plusOne 题解

Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 题目大意: 给定一个由一组数字构成的数组表示的非负整数, 对这个数进行加一操作,并将结果数组返回. 数组的首元素存储的是该非负整数的最高位. 本题比较简

[Leetcode]@python 62. Unique Paths

题目链接:https://leetcode.com/problems/unique-paths/ 题目大意:给定n.m,在mxn的矩阵中,从(0,0)走到(m-1,n-1)一共有多少种法(只能往下和往右走) 解题思路:从(0,0)到(m-1,n-1)一共要走m - 1次向下,n-1次向右.也就是在n + m - 2次中选出m-1次向下,也就是C(m + n - 2,m-1) class Solution(object): def uniquePaths(self, m, n): ""&

Pascal&#39;s triangle II Leetcode Python

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 这题和前面一题的区别在于不用返回所有解,只需要返回index对应行就行.做法和前面的一样定义一个currow 和prerow class Solu

LeetCode:(Array-189) Rotate Array

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 s