代码拉取完成,页面将自动刷新
package code;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
/*
* 380. Insert Delete GetRandom O(1)
* 题意:设计一个数据结构,插入,删除,随机获得一个元素 这三个操作的复杂度都为O(1)
* 难度:Medium
* 分类:Array, Hash Table, Design
* 思路:List 的插入和删除都是O(1), 通过hashmap绑定来使得Get也为O(1)
* Tips:和LRU哪个题类似 lc146
*/
public class lc380 {
public class RandomizedSet {
HashMap<Integer, Integer> valToInd;
List<Integer> list;
int ind = 0;
/** Initialize your data structure here. */
public RandomizedSet() {
valToInd = new HashMap<>();
list = new ArrayList<>();
}
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
public boolean insert(int val) {
if(valToInd.containsKey(val)) return false;
list.add(val);
valToInd.put(val,list.size()-1);
return true;
}
/** Removes a value from the set. Returns true if the set contained the specified element. */
public boolean remove(int val) {
int ind = valToInd.getOrDefault(val,-1);
if(ind == -1) return false;
Collections.swap(list,ind,list.size()-1);
int swappedWith = list.get(ind);
valToInd.put(swappedWith,ind);
list.remove(list.size()-1);
valToInd.remove(val);
return true;
}
/** Get a random element from the set. */
public int getRandom() {
int max = list.size();
int min = 0;
int ind = (int)(Math.random() * (max - min) + min);
return list.get(ind);
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。