zoukankan      html  css  js  c++  java
  • [leetcode]Permutations @ Python

    原题地址:https://oj.leetcode.com/problems/permutations/

    题意:

    Given a collection of numbers, return all possible permutations.

    For example,
    [1,2,3] have the following permutations:
    [1,2,3][1,3,2][2,1,3][2,3,1][3,1,2], and [3,2,1].

    解题思路:穷举一个集合的全排列。这个就是python递归巧妙的使用了。

    代码:

    class Solution:
        # @param num, a list of integer
        # @return a list of lists of integers
        def permute(self, num):
            if len(num) == 0: return []
            if len(num) == 1: return [num]
            res = []
            for i in range(len(num)):
                for j in self.permute(num[:i] + num[i+1:]):
                    res.append([num[i]] + j)
            return res
  • 相关阅读:
    例2-3
    例2-2
    例2-1
    p14
    第一次作业
    例1-1
    第二次作业(2)
    第二次作业
    第三章3-3
    第三章3-2
  • 原文地址:https://www.cnblogs.com/zuoyuan/p/3758816.html
Copyright © 2011-2022 走看看