中文题目
设计一个支持在平均 时间复杂度 O(1) 下,执行以下操作的数据结构:
insert(val)
:当元素val
不存在时返回true
,并向集合中插入该项,否则返回false
。remove(val)
:当元素val
存在时返回true
,并从集合中移除该项,否则返回false
。getRandom
:随机返回现有集合中的一项。每个元素应该有 相同的概率 被返回。
示例 :
输入: inputs = ["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"] [[], [1], [2], [2], [], [1], [2], []] 输出: [null, true, false, true, 2, true, false, 2] 解释: RandomizedSet randomSet = new RandomizedSet(); // 初始化一个空的集合 randomSet.insert(1); // 向集合中插入 1 , 返回 true 表示 1 被成功地插入 randomSet.remove(2); // 返回 false,表示集合中不存在 2 randomSet.insert(2); // 向集合中插入 2 返回 true ,集合现在包含 [1,2] randomSet.getRandom(); // getRandom 应随机返回 1 或 2 randomSet.remove(1); // 从集合中移除 1 返回 true 。集合现在包含 [2] randomSet.insert(2); // 2 已在集合中,所以返回 false randomSet.getRandom(); // 由于 2 是集合中唯一的数字,getRandom 总是返回 2
提示:
-231 <= val <= 231 - 1
- 最多进行
2 * 105
次insert
,remove
和getRandom
方法调用 - 当调用
getRandom
方法时,集合中至少有一个元素
注意:本题与主站 380 题相同:https://leetcode-cn.com/problems/insert-delete-getrandom-o1/
通过代码
高赞题解
思路和心得:
1.思想类似于对堆进行操作
(1)插入一个数时,先放到最后
(2)删除一个数时,让最后一个数字去替换
[]class RandomizedSet: def __init__(self): """ Initialize your data structure here. """ self.num_idx = dict() self.nums = [] def insert(self, val: int) -> bool: """ Inserts a value to the set. Returns true if the set did not already contain the specified element. """ if val in self.num_idx: return False self.num_idx[val] = len(self.nums) self.nums.append(val) return True def remove(self, val: int) -> bool: """ Removes a value from the set. Returns true if the set contained the specified element. """ if val not in self.num_idx: return False idx = self.num_idx[val] last_num = self.nums[-1] self.nums[idx] = last_num self.num_idx[last_num] = idx self.nums.pop() del self.num_idx[val] return True def getRandom(self) -> int: """ Get a random element from the set. """ idx = random.randint(0, len(self.nums) - 1) return self.nums[idx] # Your RandomizedSet object will be instantiated and called as such: # obj = RandomizedSet() # param_1 = obj.insert(val) # param_2 = obj.remove(val) # param_3 = obj.getRandom()
[]class RandomizedSet { public: unordered_map<int, int> num_idx; vector<int> nums; /** Initialize your data structure here. */ RandomizedSet() { } /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */ bool insert(int val) { if (num_idx.find(val) != num_idx.end()) return false; num_idx[val] = (int)nums.size(); nums.push_back(val); return true; } /** Removes a value from the set. Returns true if the set contained the specified element. */ bool remove(int val) { if (num_idx.find(val) == num_idx.end()) return false; int idx = num_idx[val]; int last_num = nums.back(); nums[idx] = last_num; num_idx[last_num] = idx; num_idx.erase(val); nums.pop_back(); return true; } /** Get a random element from the set. */ int getRandom() { int idx = rand() % (int)nums.size(); return nums[idx]; } }; /** * Your RandomizedSet object will be instantiated and called as such: * RandomizedSet* obj = new RandomizedSet(); * bool param_1 = obj->insert(val); * bool param_2 = obj->remove(val); * int param_3 = obj->getRandom(); */
[]class RandomizedSet { private Map<Integer, Integer> num_idx = new HashMap<>(); private List<Integer> nums = new ArrayList<>(); Random rand = new Random(); /** Initialize your data structure here. */ public RandomizedSet() { } /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */ public boolean insert(int val) { if (num_idx.containsKey(val) == true) return false; int idx = nums.size(); num_idx.put(val, idx); nums.add(val); return true; } /** Removes a value from the set. Returns true if the set contained the specified element. */ public boolean remove(int val) { if (num_idx.containsKey(val) == false) return false; int idx = num_idx.get(val); int last_num = nums.get(nums.size() - 1); num_idx.put(last_num, idx); nums.set(idx, last_num); num_idx.remove(val); nums.remove(nums.size() - 1); return true; } /** Get a random element from the set. */ public int getRandom() { int idx = rand.nextInt(nums.size()); return nums.get(idx); } } /** * Your RandomizedSet object will be instantiated and called as such: * RandomizedSet obj = new RandomizedSet(); * boolean param_1 = obj.insert(val); * boolean param_2 = obj.remove(val); * int param_3 = obj.getRandom(); */
统计信息
通过次数 | 提交次数 | AC比率 |
---|---|---|
3531 | 6462 | 54.6% |
提交历史
提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
---|
v1.5.1