Leetcode 46 Solution

This article provides solution to leetcode question 46 (permutations)

https://leetcode.com/problems/permutations

Solution

class Solution(object): def permute(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ if not nums: return []
ans = [[]]
for i in nums: next_ans = [] for perm in ans: for j in range(len(perm) + 1): new_perm = list(perm) new_perm.insert(j, i) next_ans.append(new_perm) ans = next_ans
return ans