choose-> explore-> unchoose paradigm is the key! below is the general pseudo code
来看看例子一:Given a collection of distinct integers, return all possible permutations.
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if not nums: #sanity check
return []
res = []
self.helper(nums, res, [])
return res
def helper(self, nums, res, item): #explore
if len(item) == len(nums):
res.app