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], and
[3,2,1].
classSolution(object): defpermute(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ result = [[]] for num in nums: new_result = [] for seq in result: for i in xrange(len(seq)+1): new_result.append(seq[:i]+[num]+seq[i:]) result = new_result return result
classSolution(object): defpermuteUnique(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ result = [[]] for num in nums: new_result = [] for seq in result: n = len(seq) for i in xrange(n,-1,-1): if i < n and seq[i] == num: break new_result.append(seq[:i]+[num]+seq[i:]) result = new_result return result