-
Notifications
You must be signed in to change notification settings - Fork 0
/
046.py
28 lines (25 loc) · 799 Bytes
/
046.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
return [[n]+p for i, n in enumerate(nums) for p in self.permute(nums[:i]+nums[i+1:])] or [[]]
# 这种dfs递归的也应该学习
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = []
lev, avail, lev_node = 0, nums, []
N = len(nums)
def dfs(lev, avail, lev_node):
if lev == N:
res.append(lev_node)
return
for i in range(len(avail)):
dfs(lev+1, avail[:i]+avail[i+1:], lev_node+[avail[i]])
dfs(lev, avail, lev_node)
return(res)