Java实现 LeetCode 432 全 O(1) 的数据结构
432. 全 O(1) 的数据结构
实现一个数据结构支持以下操作:
Inc(key) - 插入一个新的值为 1 的 key。或者使一个存在的 key 增加一,保证 key 不为空字符串。
Dec(key) - 如果这个 key 的值是 1,那么把他从数据结构中移除掉。否者使一个存在的 key 值减一。如果这个 key 不存在,这个函数不做任何事情。key 保证不为空字符串。
GetMaxKey() - 返回 key 中值最大的任意一个。如果没有元素存在,返回一个空字符串""。
GetMinKey() - 返回 key 中值最小的任意一个。如果没有元素存在,返回一个空字符串""。
挑战:以 O(1) 的时间复杂度实现所有操作。
PS:
双链表+HashMap
class AllOne {
class Node{
int value;
String key;
Node pre;
Node next;
public Node(String key, int value) {
this.key = key;
this.value = value;
}
}
HashMap<String, Node> map = new HashMap<>();
Node head;
Node tail;
/** Initialize your data structure here. */
public AllOne() {
head = new Node("", -1);
tail = new Node("", -1);
head.next = tail;
tail.pre = head;
}
// 将src插入到des的前面
public void insertPre(Node src, Node des) {
Node temp = des.pre;
temp.next = src;
src.pre = temp;
des.pre = src;
src.next = des;
}
// 将src插入到des的后面
public void insertNext(Node src, Node des) {
Node temp = des.next;
temp.pre = src;
src.next = temp;
des.next = src;
src.pre = des;
}
/** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
public void inc(String key) {
// 如果map中包含key,找到key对应的node
if(map.containsKey(key)) {
Node node = map.get(key);
node.value ++;
// 找到大于等于它的第一个Node,插入到其前面
if(node.next != tail) {
Node temp = node.next;
while(temp!=tail && temp.value<node.value) {
temp = temp.next;
}
// 连接node断开处前面的和后面的节点
node.pre.next = node.next;
node.next.pre = node.pre;
// 将node插入到temp的前面
insertPre(node, temp);
}
} else {
// 如果map中不包含key,则直接创建一个node插入到head的后面,同时将key记录到map中
Node node = new Node(key, 1);
map.put(key, node);
insertNext(node, head);
}
}
/** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
public void dec(String key) {
// map中包含key,不包含的话不管了
if(map.containsKey(key)) {
Node node = map.get(key);
// 如果key对应的node值为1,则从链表中移除节点,map中也移除该key
if(node.value == 1) {
node.pre.next = node.next;
node.next.pre = node.pre;
map.remove(key);
} else {
// 如果key对应的node值不为1,则向前寻找到它前方的第一个小于它的节点temp,插入到temp后方
node.value --;
if(node.pre != head) {
Node temp = node.pre;
while(temp!=head && temp.value>node.value) {
temp = temp.pre;
}
// 连接断开处的
node.pre.next = node.next;
node.next.pre = node.pre;
// 插入到temp后方
insertNext(node, temp);
}
}
}
}
/** Returns one of the keys with maximal value. */
public String getMaxKey() {
return tail.pre.key;
}
/** Returns one of the keys with Minimal value. */
public String getMinKey() {
return head.next.key;
}
}
/**
* Your AllOne object will be instantiated and called as such:
* AllOne obj = new AllOne();
* obj.inc(key);
* obj.dec(key);
* String param_3 = obj.getMaxKey();
* String param_4 = obj.getMinKey();
*/
Java实现 LeetCode 432 全 O(1) 的数据结构的更多相关文章
- 【系统设计】432. 全 O(1) 的数据结构
题目: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是 ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
随机推荐
- FOC 电流采样方案对比(单电阻/双电阻/三电阻)
如果本文帮到了你,帮忙点个赞: 如果本文帮到了你,帮忙点个赞: 如果本文帮到了你,帮忙点个赞: 创作不易 谢谢支持 文章目录 1 电流采样的作用 2 硬件架构 3 采样关键 4 采样方案 5 三电阻采 ...
- CF-163A Substring and Subsequence 字符串DP
Substring and Subsequence 题意 给出两个字符串s,t,求出有多少对s的子串和t的子序列相等. 思路 类似于最长公共子序列的dp数组. dp[i][j]表示s中以i为结尾的子串 ...
- Istio的流量管理(实操一)(istio 系列三)
Istio的流量管理(实操一)(istio 系列三) 使用官方的Bookinfo应用进行测试.涵盖官方文档Traffic Management章节中的请求路由,故障注入,流量迁移,TCP流量迁移,请求 ...
- Luogu P5603 小C与桌游【贪心+拓扑排序】
[Description]https://www.luogu.com.cn/problem/P5603 \(\;\) 题意可以简化为:一个不保证联通,n个点,m条边的DAG(有向无环图),构造一个拓扑 ...
- PMS学习
一,PMS的adb相关重要指令 1,adb shell dumpsys package(dump所有的系统内apk信息) 2,adb shell dumpsys package “com.androi ...
- java-> 利用IO操作与递归实现目录的复制
public class CopyDir { public static void main(String[] args) { copyDir(new File("d:\\a"), ...
- 容器技术之LXC WEB管理工具LXC WEB Panel
前一篇博文中主要说了下,lxc容器在Linux上的简单管理,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/12901493.html:今天我们来介绍下lxc的图 ...
- Kappa(cappa)系数只需要看这一篇就够了,算法到python实现
1 定义 百度百科的定义: 它是通过把所有地表真实分类中的像元总数(N)乘以混淆矩阵对角线(Xkk)的和,再减去某一类地表真实像元总数与被误分成该类像元总数之积对所有类别求和的结果,再除以总像元数的平 ...
- 5.8 Go 单元测试
5.8 Go 单元测试 如果你不想后半生的美好时光都在寻找BUG中度过,那么必须写些程序用来检测产品代码的结果和预期的一样. Go语言的测试依赖于go test测试命令和一组按约定方式编写的测试函数, ...
- Webconfig配置刷新时间,前台页面调用这个时间
<configuration> <appSettings> <add key="webpages:Version" value="2.0.0 ...