151.Insert Delete GetRandom O(1) - Duplicates allowed
来源:互联网 发布:伊豆的舞女知乎 编辑:程序博客网 时间:2024/05/04 22:09
Design a data structure that supports all following operations inaverage O(1) time.
Note: Duplicate elements are allowed.insert(val)
: Inserts an item val to the collection.remove(val)
: Removes an item val from the collection if present.getRandom
: Returns a random element from current collection of elements. The probability of each element being returned islinearly 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();
Subscribe to see which companies asked this question
分析,与前一道题目类似,不一样的地方在于,集合中可以存在重复的元素,但是在插入的时候如果已存在需要插入的val时,则返回false,但是还是要把val插入到集合中。
ArrayList<Integer> list; /** Initialize your data structure here. */ public RandomizedCollection() { list = new ArrayList<Integer>(); } /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */ public boolean insert(int val) { boolean flag = !list.contains(val); list.add(val); return flag; } /** Removes a value from the set. Returns true if the set contained the specified element. */ public boolean remove(int val) { if(!list.contains(val)){ return false; }else{ list.remove((Integer)val); return true; } } /** Get a random element from the set. */ public int getRandom() { if (list.size() == 1) {// 如果全排列的个数小于等于1个,则只需要返回base数组即可。return list.get(0);}/* 随机生成一个下标 */Random random = new Random();int i = random.nextInt(list.size());return list.get(i); }
0 0
- Insert Delete GetRandom O(1) - Duplicates allowed
- Insert Delete GetRandom O(1) - Duplicates allowed
- Insert Delete GetRandom O(1) - Duplicates allowed
- 151.Insert Delete GetRandom O(1) - Duplicates allowed
- LeetCode—381. Insert Delete GetRandom O(1) - Duplicates allowed
- 【Leetcode】Insert Delete GetRandom O(1) - Duplicates allowed
- leetcode之Insert Delete GetRandom O(1) - Duplicates allowed
- [leetcode] 381. Insert Delete GetRandom O(1) - Duplicates allowed
- 381. Insert Delete GetRandom O(1) - Duplicates allowed
- <leetcode>381. Insert Delete GetRandom O(1) - Duplicates allowed
- LeetCode Insert Delete GetRandom O(1) - Duplicates allowed
- 381. Insert Delete GetRandom O(1) - Duplicates allowed
- 381. Insert Delete GetRandom O(1) - Duplicates allowed
- LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed
- Insert Delete GetRandom O(1) - Duplicates allowed in JAVA
- LeetCode 381---Insert Delete GetRandom O(1) - Duplicates allowed
- 381. Insert Delete GetRandom O(1) - Duplicates allowed
- 381. Insert Delete GetRandom O(1) - Duplicates allowed
- 中软Java学习笔记8月11日笔记之File
- 程序员必须知道的10大基础实用算法及其讲解(转自http://www.oschina.net/question/1397765_159365)
- CSS实现三列图片等宽等间距布局
- 就别移了:社工之漏洞百出拿下朋友密码。他智商为0!
- Web编程那点事
- 151.Insert Delete GetRandom O(1) - Duplicates allowed
- 外网远程控制LED小灯的实现
- 【笔记】PMBOK第13章项目干系人管理
- java调用C/C++写的库函数 dll
- myeclipse安装其他插件后,找不到maven
- [leetcode] 【位运算】 371. Sum of Two Integers
- Alluxio 1.2.0 HA 问题集合
- Service总结
- 黄山IE修复专家V8.83免费破解版