Skip to content

Instantly share code, notes, and snippets.

@willwangcc
Last active July 14, 2017 00:58
Show Gist options
  • Save willwangcc/8a69bc4e7b9295f4948561e26aeb112c to your computer and use it in GitHub Desktop.
Save willwangcc/8a69bc4e7b9295f4948561e26aeb112c to your computer and use it in GitHub Desktop.
# Time: O(n!)
# Space: O(n!)
# 46. Permutations
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
perms = [[]]
for n in nums:
new_perms = []
for perm in perms:
for i in xrange(len(perm)+1):
new_perms.append(perm[:i] + [n] + perm[i:])
perms = new_perms
return perms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment