-
Notifications
You must be signed in to change notification settings - Fork 0
/
078.go
50 lines (44 loc) · 808 Bytes
/
078.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
package p078
import (
"sort"
)
/**
Given a set of distinct integers, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
*/
// same with subsetsWithDup 090.go
func subsets(nums []int) [][]int {
sort.Ints(nums)
ans := make([][]int, 0)
one := make([]int, len(nums))
copy(one, nums)
max := nums[len(nums)-1]
for len(one) > 0 {
onecase := make([]int, len(one))
copy(onecase, one)
ans = append(ans, onecase)
v := one[len(one)-1]
one = one[:len(one)-1]
if v < max {
index := sort.SearchInts(nums, v+1)
for index < len(nums) {
one = append(one, nums[index])
index++
}
}
}
ans = append(ans, one[:])
return ans
}