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 ...
随机推荐
- 数据结构学习:二叉查找树的概念和C语言实现
什么是二叉查找树? 二叉查找树又叫二叉排序树,缩写为BST,全称Binary Sort Tree或者Binary Search Tree. 以下定义来自百度百科: 二叉排序树或者是一棵空树,或者是具有 ...
- Android 8.1 关机充电动画(三)Android模式
system:Android 8.1 platform:RK3326/PX30 uboot kernel system/core/healthd Android 8.1 关机充电动画(一)模式选择 A ...
- HBase Filter 过滤器之QualifierFilter详解
前言:本文详细介绍了 HBase QualifierFilter 过滤器 Java&Shell API 的使用,并贴出了相关示例代码以供参考.QualifierFilter 基于列名进行过滤, ...
- .netcore 部署时遇到413 Request Entity Too Large 和 413Payload Too Large 的问题
.netcore3.1 遇到一个webapi 上传大文件问题 首先,在kestrel模式调试模式下上传 会报错413, 需要在三个地方添加 1.startup中 这里设置的2g最大值 2.在progr ...
- Java并发II
Java并发 J.U.C图 一.线程的安全性 当多个线程访问某个类的时候,不管运行环境采用何种方式调度或者这些线程如何交替执行,并且在主调代码中不需要任何额外的同步或者协同,这个类都可以表现出正确的行 ...
- git命令之切换分支
Git一般有很多分支,我们clone到本地的一般都是master分支,如何进行分支的切换呢?那么下面带大家简单的看看如何通过命令来切换: 1.查看远程仓库及本地的所有分支 命令:git branch ...
- 全网最全Docker命令详解
由于最近在学习Docker,在这里把有关Docker的命令做一个集合,方便后面查看: # docker --help Usage: docker [OPTIONS] COMMAND [arg...] ...
- 查找算法----二分查找与hash查找
二分查找 有序列表对于我们的实现搜索是很有用的.在顺序查找中,当我们与第一个元素进行比较时,如果第一个元素不是我们要查找的,则最多还有 n-1 个元素需要进行比较. 二分查找则是从中间元素开始,而不是 ...
- 02 Redis数据结构基础
一.客户端命令行参数 1.-x 从标准输入读取一个参数,等价于set k v [root@localhost etc]# echo -en 'v1'|redis-cli -a foobared -x ...
- 在ORACLE中实现SELECT TOP N的方法----[转]
1.在ORACLE中实现SELECT TOP N 由于ORACLE不支持SELECT TOP语句,所以在ORACLE中经常是用ORDER BY跟ROWNUM的组合来实现SELECT TOP N的查询. ...