forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.go
43 lines (39 loc) · 847 Bytes
/
Solution.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
type CombinationIterator struct {
cs []string
idx int
}
func Constructor(characters string, combinationLength int) CombinationIterator {
t := []byte{}
n := len(characters)
cs := []string{}
var dfs func(int)
dfs = func(i int) {
if len(t) == combinationLength {
cs = append(cs, string(t))
return
}
if i == n {
return
}
t = append(t, characters[i])
dfs(i + 1)
t = t[:len(t)-1]
dfs(i + 1)
}
dfs(0)
return CombinationIterator{cs, 0}
}
func (this *CombinationIterator) Next() string {
ans := this.cs[this.idx]
this.idx++
return ans
}
func (this *CombinationIterator) HasNext() bool {
return this.idx < len(this.cs)
}
/**
* Your CombinationIterator object will be instantiated and called as such:
* obj := Constructor(characters, combinationLength);
* param_1 := obj.Next();
* param_2 := obj.HasNext();
*/