[46] Permutations
https://leetcode.com/problems/permutations/description/
- algorithms
- Medium (51.45%)
- Source Code: 46.permutations.py
- Total Accepted: 362.1K
- Total Submissions: 665.1K
- Testcase Example: '[1,2,3]'
Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]
python
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if not nums: return []
result = []
lenn = len(nums)
pool = list(range(lenn))
def enum_value(current, pool):
if len(pool) == 1:
current.append(nums[pool[0]])
result.append(current)
return
else:
for p in pool:
tmp = pool[:]
tmp.remove(p)
enum_value(current + [nums[p]], tmp)
enum_value([], pool)
return result