-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path46.py
More file actions
49 lines (41 loc) · 1.45 KB
/
46.py
File metadata and controls
49 lines (41 loc) · 1.45 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
__________________________________________________________________________________________________
sample 40 ms submission
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = []
visited = set([])
def dfs(nums, path, res, visited):
if len(path) == len(nums):
res.append(path + [])
return
for i in range(0, len(nums)):
if i not in visited:
visited.add(i)
dfs(nums, path+[nums[i]], res, visited)
visited.discard(i)
dfs(nums, [], res, visited)
return res
__________________________________________________________________________________________________
sample 12664 kb submission
class Solution:
def permute(self, nums):
ans = []
def dfs(index):
if index == len(nums):
ans.append(nums[:])
return
for i in range(index, len(nums)):
nums[i], nums[index] = nums[index], nums[i]
dfs(index + 1)
nums[i], nums[index] = nums[index], nums[i]
dfs(0)
return ans
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
__________________________________________________________________________________________________