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] ]
class Solution:def permute(self, nums):""":type nums: List[int]:rtype: List[List[int]]"""res = []s = set(nums)def gen(l, added):if len(l) is len(nums):res.append(l[:])else:for i in s:if not i in added:l.append(i)added.add(i)gen(l, added)l.pop()added.remove(i)gen([], set())return res

浙公网安备 33010602011771号