|
| 1 | +/* |
| 2 | + * @lc app=leetcode.cn id=380 lang=golang |
| 3 | + * |
| 4 | + * [380] 常数时间插入、删除和获取随机元素 |
| 5 | + * |
| 6 | + * https://leetcode-cn.com/problems/insert-delete-getrandom-o1/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Medium (46.26%) |
| 10 | + * Likes: 58 |
| 11 | + * Dislikes: 0 |
| 12 | + * Total Accepted: 6.4K |
| 13 | + * Total Submissions: 13.7K |
| 14 | + * Testcase Example: '["RandomizedSet","insert","remove","insert","getRandom","remove","insert","getRandom"]\n[[],[1],[2],[2],[],[1],[2],[]]' |
| 15 | + * |
| 16 | + * 设计一个支持在平均 时间复杂度 O(1) 下,执行以下操作的数据结构。 |
| 17 | + * |
| 18 | + * |
| 19 | + * insert(val):当元素 val 不存在时,向集合中插入该项。 |
| 20 | + * remove(val):元素 val 存在时,从集合中移除该项。 |
| 21 | + * getRandom:随机返回现有集合中的一项。每个元素应该有相同的概率被返回。 |
| 22 | + * |
| 23 | + * |
| 24 | + * 示例 : |
| 25 | + * |
| 26 | + * |
| 27 | + * // 初始化一个空的集合。 |
| 28 | + * RandomizedSet randomSet = new RandomizedSet(); |
| 29 | + * |
| 30 | + * // 向集合中插入 1 。返回 true 表示 1 被成功地插入。 |
| 31 | + * randomSet.insert(1); |
| 32 | + * |
| 33 | + * // 返回 false ,表示集合中不存在 2 。 |
| 34 | + * randomSet.remove(2); |
| 35 | + * |
| 36 | + * // 向集合中插入 2 。返回 true 。集合现在包含 [1,2] 。 |
| 37 | + * randomSet.insert(2); |
| 38 | + * |
| 39 | + * // getRandom 应随机返回 1 或 2 。 |
| 40 | + * randomSet.getRandom(); |
| 41 | + * |
| 42 | + * // 从集合中移除 1 ,返回 true 。集合现在包含 [2] 。 |
| 43 | + * randomSet.remove(1); |
| 44 | + * |
| 45 | + * // 2 已在集合中,所以返回 false 。 |
| 46 | + * randomSet.insert(2); |
| 47 | + * |
| 48 | + * // 由于 2 是集合中唯一的数字,getRandom 总是返回 2 。 |
| 49 | + * randomSet.getRandom(); |
| 50 | + * |
| 51 | + * |
| 52 | + */ |
| 53 | + |
| 54 | +/* |
| 55 | + * 1. 为了达到O(1)的插入删除,需要使用hash |
| 56 | + * 2. 为了达到O(1)的随机数,需要用数组保存,然后随机取数组长度内的下标 |
| 57 | + * 3. 增加数组之后,增加可以直接增加到数组末尾,删除的话可以交换被删元素与数组尾部,这样只需要移动一个元素 |
| 58 | + */ |
| 59 | + |
| 60 | +// @lc code=start |
| 61 | +import ( |
| 62 | + "math/rand" |
| 63 | +) |
| 64 | + |
| 65 | +type RandomizedSet struct { |
| 66 | + Data map[int]int // k为传入val,v为下标 |
| 67 | + Index []int |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +/** Initialize your data structure here. */ |
| 72 | +func Constructor() RandomizedSet { |
| 73 | + return RandomizedSet{ |
| 74 | + Data: map[int]int{}, |
| 75 | + Index: []int{}, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */ |
| 81 | +func (this *RandomizedSet) Insert(val int) bool { |
| 82 | + if _, ok := this.Data[val]; ok { |
| 83 | + return false |
| 84 | + } |
| 85 | + this.Index = append(this.Index, val) |
| 86 | + this.Data[val] = len(this.Index) - 1 |
| 87 | + return true |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +/** Removes a value from the set. Returns true if the set contained the specified element. */ |
| 92 | +func (this *RandomizedSet) Remove(val int) bool { |
| 93 | + idx, ok := this.Data[val] |
| 94 | + if !ok { |
| 95 | + return false |
| 96 | + } |
| 97 | + last := len(this.Index) - 1 |
| 98 | + if idx < last { // 与队尾交互 |
| 99 | + this.Data[this.Index[last]] = idx |
| 100 | + this.Index[idx] = this.Index[last] |
| 101 | + } |
| 102 | + delete(this.Data, val) |
| 103 | + this.Index = this.Index[:last] |
| 104 | + return true |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +/** Get a random element from the set. */ |
| 109 | +func (this *RandomizedSet) GetRandom() int { |
| 110 | + idx := rand.Intn(len(this.Index)) |
| 111 | + return this.Index[idx] |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +/** |
| 116 | + * Your RandomizedSet object will be instantiated and called as such: |
| 117 | + * obj := Constructor(); |
| 118 | + * param_1 := obj.Insert(val); |
| 119 | + * param_2 := obj.Remove(val); |
| 120 | + * param_3 := obj.GetRandom(); |
| 121 | + */ |
| 122 | +// @lc code=end |
| 123 | + |
0 commit comments