[抄题]:
Design a data structure that supports all following operations in average O(1) time.
-
insert(val)
: Inserts an item val to the set if not already present. -
remove(val)
: Removes an item val from the set if present. -
getRandom
: Returns a random element from current set of elements. Each element must have the same probability of being returned.
Example:
// Init an empty set.
RandomizedSet randomSet = new RandomizedSet(); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
randomSet.insert(1); // Returns false as 2 does not exist in the set.
randomSet.remove(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
randomSet.insert(2); // getRandom should return either 1 or 2 randomly.
randomSet.getRandom(); // Removes 1 from the set, returns true. Set now contains [2].
randomSet.remove(1); // 2 was already in the set, so return false.
randomSet.insert(2); // Since 2 is the only number in the set, getRandom always return 2.
randomSet.getRandom();
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以前不知道这类题在干嘛:其实就是自己用一种数据结构,实现另一种数据结构
[一句话思路]:
为了保证随机生成的概率均衡,数组必须保持满格,只能从最后一位删除
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 提前备注,别忘了加return
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
其实就是自己用一种数据结构,实现另一种数据结构
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
新建随机对象、利用随机数方法:
java.util.Random rand = new java.util.Random(); 类名首字母大写
nums.get( rand.nextInt(nums.size()) );
[算法思想:递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class RandomizedCollection { /** Initialize your data structure here. */
HashMap<Integer, Set<Integer>> map;
ArrayList<Integer> nums;
java.util.Random rand = new java.util.Random(); public RandomizedCollection() {
map = new HashMap<Integer, Set<Integer>>();
nums = new ArrayList<>();
} /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
public boolean insert(int val) {
boolean contain = map.containsKey(val);
if (!contain) map.put(val, new HashSet<Integer>()); map.get(val).add(nums.size());
nums.add(val); return !contain;
} /** Removes a value from the collection. Returns true if the collection contained the specified element. */
public boolean remove(int val) {
boolean contain = map.containsKey(val);
if (!contain) return false; int loc = map.get(val).iterator().next();
//renew location
if (loc < nums.size() - 1) {
int lastone = nums.get(nums.size() - 1);
nums.set(loc, lastone);
map.get(lastone).add(loc);
map.get(lastone).remove(nums.size() - 1);
} //remove, if key is empty
nums.remove(nums.size() - 1);
map.get(val).remove(loc);
if (map.get(val).isEmpty()) map.remove(val); //return
return true;
} /** Get a random element from the collection. */
public int getRandom() {
return nums.get(rand.nextInt(nums.size()));
}
} /**
* Your RandomizedCollection object will be instantiated and called as such:
* RandomizedCollection obj = new RandomizedCollection();
* boolean param_1 = obj.insert(val);
* boolean param_2 = obj.remove(val);
* int param_3 = obj.getRandom();
*/