1 Star 0 Fork 0

表情扭曲 / leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc380.java 1.82 KB
一键复制 编辑 原始数据 按行查看 历史
liu13 提交于 2019-07-03 18:22 . 20190703
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);
}
}
}
1
https://gitee.com/abfantasy/leetcode.git
git@gitee.com:abfantasy/leetcode.git
abfantasy
leetcode
leetcode
master

搜索帮助