47 Permutations II
[
[1,1,2],
[1,2,1],
[2,1,1]
]class Solution:
def permuteUnique(self, nums):
sol = [[]]
for n in nums:
next_sol = []
for prev in sol:
# ab, ba
for i in range(0, len(prev) + 1):
# cab, acb, abc
next_sol.append(prev[:i] + [n] + prev[i:])
# this line identifies duplicates
if i < len(prev) and prev[i] == n:
break
sol = next_sol
return solLast updated