Last active
July 14, 2017 00:58
-
-
Save willwangcc/8a69bc4e7b9295f4948561e26aeb112c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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