Skip to content

Commit a861a95

Browse files
aQuaaQua
aQua
authored and
aQua
committed
381 runtime error
1 parent 9e94095 commit a861a95

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# [381. Insert Delete GetRandom O(1) - Duplicates allowed](https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/)
2+
3+
## 题目
4+
Design a data structure that supports all following operations in average O(1) time.
5+
Note: Duplicate elements are allowed.
6+
7+
1. insert(val): Inserts an item val to the collection.
8+
1. remove(val): Removes an item val from the collection if present.
9+
1. getRandom: Returns a random element from current collection of elements. The probability of each element being returned is linearly related to the number of same value the collection contains.
10+
11+
12+
13+
Example:
14+
```
15+
// Init an empty collection.
16+
RandomizedCollection collection = new RandomizedCollection();
17+
18+
// Inserts 1 to the collection. Returns true as the collection did not contain 1.
19+
collection.insert(1);
20+
21+
// Inserts another 1 to the collection. Returns false as the collection contained 1. Collection now contains [1,1].
22+
collection.insert(1);
23+
24+
// Inserts 2 to the collection, returns true. Collection now contains [1,1,2].
25+
collection.insert(2);
26+
27+
// getRandom should return 1 with the probability 2/3, and returns 2 with the probability 1/3.
28+
collection.getRandom();
29+
30+
// Removes 1 from the collection, returns true. Collection now contains [1,2].
31+
collection.remove(1);
32+
33+
// getRandom should return 1 and 2 both equally likely.
34+
collection.getRandom();
35+
```
36+
## 解题思路
37+
38+
见程序注释
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package Problem0381
2+
3+
import "math/rand"
4+
5+
// RandomizedCollection 是一个随机获取的集合
6+
type RandomizedCollection struct {
7+
a []int
8+
idx map[int][]int // idx 的 v 是 k 在 a 中的位置。
9+
}
10+
11+
// Constructor returns RandomizedSet
12+
/** Initialize your data structure here. */
13+
func Constructor() RandomizedCollection {
14+
return RandomizedCollection{
15+
a: []int{},
16+
idx: make(map[int][]int),
17+
}
18+
}
19+
20+
// Insert 插入数据
21+
/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
22+
func (r *RandomizedCollection) Insert(val int) bool {
23+
res := false
24+
if _, ok := r.idx[val]; !ok {
25+
res = true
26+
}
27+
r.a = append(r.a, val)
28+
r.idx[val] = append(r.idx[val], len(r.a)-1)
29+
return res
30+
}
31+
32+
// Remove 删除数据
33+
/** Removes a value from the collection. Returns true if the collection contained the specified element. */
34+
func (r *RandomizedCollection) Remove(val int) bool {
35+
if _, ok := r.idx[val]; !ok {
36+
return false
37+
}
38+
39+
last := r.a[len(r.a)-1]
40+
lenOfLast := len(r.idx[last])
41+
lenOfVal := len(r.idx[val])
42+
// 把 a 的最后一个数,放入最后一个 val 的位置
43+
r.a[r.idx[val][lenOfVal-1]] = last
44+
r.idx[last][lenOfLast-1] = r.idx[val][lenOfVal-1]
45+
46+
// 删除最后一个数
47+
r.a = r.a[:len(r.a)-1]
48+
// 在 r.idx 中删除 val 的记录
49+
if lenOfVal == 1 {
50+
delete(r.idx, val)
51+
} else {
52+
r.idx[val] = r.idx[val][:lenOfVal-1]
53+
}
54+
55+
return true
56+
}
57+
58+
// GetRandom 获取随机数据
59+
/** Get a random element from the collection. */
60+
func (r *RandomizedCollection) GetRandom() int {
61+
return r.a[rand.Intn(len(r.a))]
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package Problem0381
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_Problem0381(t *testing.T) {
10+
ast := assert.New(t)
11+
12+
rs := Constructor()
13+
14+
ast.True(rs.Insert(1), "插入1")
15+
16+
ast.False(rs.Remove(2), "删除2")
17+
18+
ast.True(rs.Insert(2), "插入2")
19+
20+
ast.Contains([]int{1, 2}, rs.GetRandom(), "返回1或2")
21+
22+
ast.True(rs.Remove(1), "删除1")
23+
24+
ast.False(rs.Insert(2), "再一次插入2")
25+
26+
ast.Equal(2, rs.GetRandom(), "从只有2的集合中随机取出2")
27+
28+
length := 100
29+
result := make([]int, length)
30+
for i := 0; i < 100; i++ {
31+
rs.Insert(i)
32+
result[i] = i
33+
}
34+
ast.Contains(result, rs.GetRandom(), "随机获取0~100之间的数")
35+
}

0 commit comments

Comments
 (0)