-
Notifications
You must be signed in to change notification settings - Fork 0
/
46.permutations.py
38 lines (29 loc) · 923 Bytes
/
46.permutations.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
29
30
31
32
33
34
35
36
37
#
# @lc app=leetcode id=46 lang=python3
#
# [46] Permutations
#
# @lc code=start
from collections import deque
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
nums, n = deque(nums), len(nums)
temp = []
res = []
def helper(last_not_chosen_number):
if len(temp) == n:
res.append(temp[:])
return
if nums[0] == last_not_chosen_number:
return # return what?
# choose the head
temp.append(nums.popleft())
helper(None) # core
nums.appendleft(temp.pop())
# not choose the head
nums.append(nums.popleft())
helper(last_not_chosen_number if last_not_chosen_number != None else nums[-1])
nums.appendleft(nums.pop())
helper(None)
return res
# @lc code=end