-
Notifications
You must be signed in to change notification settings - Fork 0
/
question30.go
47 lines (38 loc) · 884 Bytes
/
question30.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
package chapter05
import (
"crypto/rand"
"math/big"
)
type RandomSet interface {
insert(value int) bool
remove(value int) bool
getRandom() int
}
type randomSet struct {
values []int
position map[int]int
}
func (r *randomSet) insert(value int) bool {
if _, ok := r.position[value]; !ok {
r.values = append(r.values, value)
r.position[value] = len(r.values) - 1
return true
}
return false
}
func (r *randomSet) remove(value int) bool {
if d, ok := r.position[value]; ok {
delete(r.position, value)
l := len(r.values) - 1
// Switch the value and delete the last value to get a O(1) operation.
r.position[r.values[l]] = d
r.values[d] = r.values[l]
r.values = r.values[:l]
return true
}
return false
}
func (r *randomSet) getRandom() int {
random, _ := rand.Int(rand.Reader, big.NewInt(int64(len(r.values))))
return r.values[random.Int64()]
}