[抄题]:

Design a data structure that supports all following operations in average O(1) time.

Note: Duplicate elements are allowed.

  1. insert(val): Inserts an item val to the collection.
  2. remove(val): Removes an item val from the collection if present.
  3. 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.

Example:

// Init an empty collection.
RandomizedCollection collection = new RandomizedCollection(); // Inserts 1 to the collection. Returns true as the collection did not contain 1.
collection.insert(1); // Inserts another 1 to the collection. Returns false as the collection contained 1. Collection now contains [1,1].
collection.insert(1); // Inserts 2 to the collection, returns true. Collection now contains [1,1,2].
collection.insert(2); // getRandom should return 1 with the probability 2/3, and returns 2 with the probability 1/3.
collection.getRandom(); // Removes 1 from the collection, returns true. Collection now contains [1,2].
collection.remove(1); // getRandom should return 1 and 2 both equally likely.
collection.getRandom();

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

如果hashmap的value为空,则必须把key也删掉

[思维问题]:

[一句话思路]:

用set存location, 然后还是先移动 后删除

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 提前判断存在性还是需要的,因为如果不存在时,需要初始化一个hashset
  2. map中存的是nums数组的大小,表示目前元素插入成为了新数组最后一位:nums.size()
  3. nums.remove(index,不是内容)

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

hashset可以分区

[复杂度]:Time complexity: O() Space complexity: O()

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[算法思想:递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[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();
*/

最新文章

  1. MarkdownPad2.5 注册码
  2. 配置redis外网可访问,并只允许指定的ip可访问redis
  3. ob_clean()
  4. IDEA 从SVN检出项目相关配置
  5. php中at @符号的作用使用说明
  6. Http状态码的种类及含义
  7. Large-Scale Deployment of SharePoint Team Services
  8. bootstrap 之 列表组件使用
  9. 装双系统(win7/win8/ubuntu)问题总结
  10. chapter 13_0 元方法
  11. linux_base_commond_one
  12. javascript右键菜单分析
  13. 初识Java多线程编程
  14. python之zip打包
  15. [转]Jsoup(一)Jsoup详解(官方)
  16. 移动端H5拍照代码实现及外网部署
  17. django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call
  18. [转] 理解 LSTM 网络
  19. asp.net页面传值方法汇总
  20. java数据结构和算法学习笔记

热门文章

  1. hadoop之 HDFS-Hadoop存档
  2. linux通过wget直接下载jdk,避免用户验证
  3. RK3288 摄像头左右镜像
  4. winform下实现pictureBox全屏播放
  5. Ubuntu---samba(安装、配置、使用)OK
  6. 查看Google Cloud的IP地址段
  7. golang的最简单的文件浏览web服务器
  8. 如何安装nginx第三方模块
  9. python稀疏矩阵得到每列最大k项的值,对list内为类对象的排序(scipy.sparse.csr.csr_matrix)
  10. Redis: temple