Title: Permutations Source: leetcode.com
Given a collection of 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]
.
Python solution
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 |
''' https://leetcode.com/problems/permutations/ ''' class Solution(object): def __init__(self): self.lst = [] def helper(self, curr, rem): if not rem: self.lst.append(curr) else: ln = len(rem) for i in range(0, ln): self.helper(curr + [rem[i]], rem[:i] + rem[i+1:]) def permute(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ if not nums: return self.lst self.helper([], nums) return self.lst |