File tree Expand file tree Collapse file tree 1 file changed +2
-3
lines changed
Algorithms/0380.insert-delete-getrandom-o1 Expand file tree Collapse file tree 1 file changed +2
-3
lines changed Original file line number Diff line number Diff line change 1
1
package Problem0380
2
2
3
3
import "math/rand"
4
- import "fmt"
5
4
6
5
// RandomizedSet 是一个随机获取的集合
7
6
type RandomizedSet struct {
@@ -38,9 +37,10 @@ func (r *RandomizedSet) Remove(val int) bool {
38
37
39
38
// 把 a 的最后一个数,放入待删除的数的位置
40
39
r.a [r.idx [val ]] = r .a [len (r .a )- 1 ]
40
+ r .idx [r .a [len (r .a )- 1 ]] = r .idx [val ]
41
41
// 删除最后一个数
42
42
r .a = r .a [:len (r .a )- 1 ]
43
- // 在 r.m 中删除 val 的记录
43
+ // 在 r.idx 中删除 val 的记录
44
44
delete (r .idx , val )
45
45
46
46
return true
@@ -49,7 +49,6 @@ func (r *RandomizedSet) Remove(val int) bool {
49
49
// GetRandom 获取随机数据
50
50
/** Get a random element from the set. */
51
51
func (r * RandomizedSet ) GetRandom () int {
52
- fmt .Println (r .a )
53
52
return r .a [rand .Intn (len (r .a ))]
54
53
}
55
54
You can’t perform that action at this time.
0 commit comments