-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutations-ii.go
51 lines (41 loc) · 1.07 KB
/
permutations-ii.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
"sort"
)
// source: https://leetcode.com/problems/permutations-ii/
func permuteUnique(nums []int) [][]int {
var res = make([][]int, 0)
var used = make([]bool, len(nums))
var util func(nums, cur []int)
util = func(nums, cur []int) {
if len(cur) == len(nums) {
res = append(res, append([]int{}, cur...))
return
}
for i := range nums {
if used[i] || (i > 0 && nums[i] == nums[i-1] && !used[i-1]) {
continue
}
used[i] = true
cur = append(cur, nums[i])
util(nums, cur)
cur = cur[:len(cur)-1]
used[i] = false
}
}
sort.Ints(nums)
util(nums, []int{})
return res
}
func main() {
// Example 3
var nums3 = []int{1, 1, 2, 2}
fmt.Println("Expected: [[1,1,2,2],[1,2,1,2],[1,2,2,1],[2,1,1,2],[2,1,2,1],[2,2,1,1]] Output: ", permuteUnique(nums3))
// Example 1
var nums1 = []int{1, 1, 2}
fmt.Println("Expected: [[1,1,2], [1,2,1], [2,1,1]] Output: ", permuteUnique(nums1))
// Example 2
var nums2 = []int{1, 2, 3}
fmt.Println("Expected: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Output: ", permuteUnique(nums2))
}