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) 的数据结构的更多相关文章

  1. 【系统设计】432. 全 O(1) 的数据结构

    题目: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是 ...

  2. 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 ...

  3. 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. ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 假如用王者荣耀的方式学习webpack

    英雄介绍 崴博.派克诞生于遥远西方的勇士之地,拥有着高超的机械技艺,善于运用各种工具来实现一些看似不可能完成的事.游历王者大陆时机缘巧合遇到了年轻的墨子,与之成为好友.后协助大宗师墨子建造了大陆第一雄 ...

  2. STM32 CubeIDE无法进行调试的问题

    解决了由于一个很容易忽视的细节最终导致系统配置存在错误造成STM32 CubeIDE无法进行调试的问题: 文章目录 来龙去脉 解决方案 反思 来龙去脉 在享受CubeIDE快速和便捷的服务之后,生成了 ...

  3. [poj 1743] Musical Theme 后缀数组 or hash

    Musical Theme 题意 给出n个1-88组成的音符,让找出一个最长的连续子序列,满足以下条件: 长度大于5 不重叠的出现两次(这里的出现可以经过变调,即这个序列的每个数字全都加上一个整数x) ...

  4. CF#633C Spy Syndrome 2 DP+二分+hash

    Spy Syndrome 2 题意 现在对某个英文句子,进行加密: 把所有的字母变成小写字母 把所有的单词反过来 去掉单词之间的空格 比如:Kira is childish and he hates ...

  5. static RMQ

    RMQ问题:对于长度为N的序列,询问区间[L,R]中的最值 RMQ(Range Minimum/Maximum Query),即区间最值查询. 常见解法: 1.朴素搜索 2.线段树 3.DP 4.神奇 ...

  6. html中require.config 缓存问题

    在html中,require的官方基本用法如下: <!DOCTYPE html> <html> <head> <title>My Sample Proj ...

  7. loadrunner Error -27985问题

    错误提示:Error -27985: There is no context for HTML-based functions. A previous function may not have us ...

  8. python之logging基础入门

    博客学习至:https://www.cnblogs.com/Nicholas0707/p/9021672.html#_label0 https://www.cnblogs.com/dream66/p/ ...

  9. 微信小程序canvas canvasGetImageData方法真机获得数据显示到image为空白

    方法 wx.canvasGetImageData的参数 width,height 只能是整数

  10. EOS基础全家桶(十二)智能合约IDE-VSCode

    简介 上一篇我们介绍了EOS的专用IDE工具EOS Studio,该工具的优势是简单,易上手,但是灵活性低,且对系统资源开销大,依赖多,容易出现功能异常.那么我们开发人员最容易使用的,可能还是深度定制 ...