当前位置:网站首页>Leetcode - 380 o (1) time to insert, delete and get random elements (design hash table + array)
Leetcode - 380 o (1) time to insert, delete and get random elements (design hash table + array)
2022-07-25 15:40:00 【Cute at the age of three @d】


Hashtable +list


class RandomizedSet {
// Put the value in list in In order to random access
private List<Integer> list;
//map Of key Is the value map Of val yes Values in list Position in
// For convenience in o(1) Within time Insert delete value
private Map<Integer,Integer> map;
public RandomizedSet() {
list = new ArrayList<>();
map = new HashMap<>();
}
public boolean insert(int val) {
// If the value already exists
if(map.containsKey(val) == true)
return false;
else{
// Newly added value index
int index = list.size();
// Added to the list
list.add(val);
// Added to the map
map.put(val,index);
return true;
}
}
public boolean remove(int val) {
//val non-existent Delete failed
if(map.containsKey(val) == false)
return false;
else{
//list The subscript of the last number
int lasti = list.size()-1;
//list The value of the last number
int lastv = list.get(lasti);
//val stay list Subscript in
int vali = map.get(val);
// take list The last number of is placed in val stay list Location
list.set(vali,lastv);
// modify list The last number of is map Subscript in
map.put(lastv,vali);
// Delete list The last number in
list.remove(lasti);
// stay map Delete in val
map.remove(val);
return true;
}
}
public int getRandom() {
int n = list.size();
// produce 0 - n-1 The random integer of the range
return list.get((int)(Math.random()*n));
}
}
/** * 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(); */
边栏推荐
- PageHelper does not take effect, and SQL does not automatically add limit
- 伤透脑筋的CPU 上下文切换
- No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac
- 2021 Shanghai match-h-two point answer
- 盒子躲避鼠标
- Phased summary of the research and development of the "library management system -" borrowing and returning "module
- Cf685b find the center of gravity of each subtree of a rooted tree
- Gary marcus: learning a language is more difficult than you think
- CF566A-贪心+字典树
- Xcode added mobileprovision certificate file error: Xcode encoded an error
猜你喜欢
随机推荐
Brain racking CPU context switching
C # fine sorting knowledge points 9 Set 2 (recommended Collection)
2021 Jiangsu race a Array line segment tree, maintain value range, Euler power reduction
In depth: micro and macro tasks
Games101 review: 3D transformation
对this对象的理解
盒子躲避鼠标
Use cpolar to build a business website (how to buy a domain name)
小波变换--dwt2 与wavedec2
Gary Marcus: 学习语言比你想象的更难
Solve the vender-base.66c6fc1c0b393478adf7.js:6 typeerror: cannot read property 'validate' of undefined problem
LeetCode - 622 设计循环队列 (设计)
自定义注解校验API参数电话号
Qtime definition (manual waste utilization is simple and beautiful)
C#精挑整理知识要点10 泛型(建议收藏)
2021 Shanghai match-b-ranked DP
PAT甲级1152 Google Recruitment (20 分)
Week303 of leetcode
ZOJ - 4114 Flipping Game-dp,合理状态表示
C#精挑整理知识要点12 异常处理(建议收藏)









