47. Permutations II
Problem description:
Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.
Example 1:1
2
3
4
5Input: nums = [1,1,2]
Output:
[[1,1,2],
[1,2,1],
[2,1,1]]
Example 2:1
2Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Solution:
1 | class Solution: |
output of the code:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17[1]
[1, 1]
[1, 1, 1]
[1, 1, 1, 2]
---
[1, 1, 2]
[1, 1, 2, 1]
---
[1, 2]
[1, 2, 1]
[1, 2, 1, 1]
---
[2]
[2, 1]
[2, 1, 1]
[2, 1, 1, 1]
---
time complexity: $O()$
space complexity: $O()$
reference:
related problem: