46. Permutations (全排列)

Given a collection of distinct 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],
  [3,2,1]
]

 

运用递归。 1234为例子

for  i in 1234:

  1  +  234(的全排列)

  2  +  134(的全排列)

  3  +  124(的全排列)

  4   + 123 (的全排列)

 

 

 

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:

        res = []
        def batracking(path,used):
            #print(path)
            if len(nums) == len(path):
                res.append(path.copy())
                return 
            for i in range(0,len(nums)):
                if used[i]:
                    continue
                used[i] = 1
                batracking(path+[nums[i]],used)
                used[i] = 0
        used = [0] * len(nums)
        batracking([],used)
        return res

 

 

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:

        res = []
        def batracking(path):
            #print(path)
            if len(nums) == len(path):
                res.append(path.copy())
                return 
            for i in range(0,len(nums)):
                if nums[i] in set(path):
                    continue
                batracking(path+[nums[i]])
        batracking([])
        return res

 

 

 

 

 

 1 class Solution(object):
 2     def __init__(self):
 3         self.res = []
 4 
 5     def permute(self, nums):
 6         """
 7         :type nums: List[int]
 8         :rtype: List[List[int]]
 9         """
10         self.help(nums, 0, len(nums))
11 
12         return self.res
13 
14     def help(self, a, lo, hi):
15         if(lo == hi):
16             self.res.append(a[0:hi])
17         for i in range(lo, hi):
18             self.swap(a, i, lo)
19             self.help(a, lo + 1, hi)
20             self.swap(a, i, lo)
21     def swap(self, a, i, j):
22         temp = a[i]
23         a[i] = a[j]
24         a[j] = temp

 

 

非递归

 1 class Solution(object):
 2  
 3 
 4     def permute(self, nums):
 5         """
 6         :type nums: List[int]
 7         :rtype: List[List[int]]
 8         """
 9         nums = sorted(nums.copy())
10         res = [nums.copy()]
11 
12         n = self.next_permute(nums)
13         while True:
14             
15             if n is None:
16                 break
17             res.append(n)
18             n = self.next_permute(n.copy())
19 
20         return res
21 
22     def next_permute(self, a):
23         i = -1
24         for index in range(0, len(a) - 1)[::-1]:
25             if(a[index] < a[index + 1]):
26                 i = index
27                 break
28         if i==-1:
29             return None       
30         min_i = a.index(min([k for k in a[i+1:] if k >a[i]]))
31         self.swap(a, i, min_i)
32         an = a[:i + 1] + self.revers(a[i + 1:])
33         return an
34 
35     def revers(self, x):
36         for i in range(int(len(x) / 2)):
37             temp = x[i]
38             x[i] = x[len(x) - i - 1]
39             x[len(x) - i - 1] = temp
40         return x
41 
42     def swap(self, a, i, j):
43         temp = a[i]
44         a[i] = a[j]
45         a[j] = temp

 

posted @ 2018-02-14 19:49  乐乐章  阅读(1648)  评论(0编辑  收藏  举报