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. Day_12【集合】扩展案例4_判断字符串每一个字符出现的次数

    分析以下需求,并用代码实现 1.利用键盘录入,输入一个字符串 2.统计该字符串中各个字符的数量(提示:字符不用排序) 3.如: 用户输入字符串 "If~you-want~to~change- ...

  2. JDK/Java 14 发布

    3 月 17 日,JDK/Java 14 正式 GA. 此版本包含的 JEP(Java/JDK Enhancement Proposals,JDK 增强提案)比 Java 12 和 13 加起来的还要 ...

  3. 三个方法生成python的exe文件

    背景:用的python3.8 方法一:用cmd 输入[pip3 install pyinstaller] 上一条指令报错 事实上,在python3.8版本时,输入pip也会显示是无法支持的语句,需要用 ...

  4. 编写HTML和CSS几点心得

    HTML代码优化 表单域用fieldset包起来,并用legend说明其用途(注意在css初始化的时候把fieldset的border设为0,把legend的display设为none) 每个inpu ...

  5. 【雕爷学编程】Arduino动手做(63)---TCS3200D颜色识别传感器

    37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的.鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为 ...

  6. nginx配置之代理功能

    nginx代理功能 至少需要两台主机: 代理机配置----如下: yum安装的/etc/nginx/nginx.conf location / { #服务机的ip端口 proxy_pass http: ...

  7. Django视图函数函数之视图装饰器

    FBV模式装饰器: 普通函数的装饰器(语法糖@) views.py from django.shortcuts import render def wrapper(f): def inner(*arg ...

  8. MySQL 选错索引的原因?

    MySQL 中,可以为某张表指定多个索引,但在语句具体执行时,选用哪个索引是由 MySQL 中执行器确定的.那么执行器选择索引的原则是什么,以及会不会出现选错索引的情况呢? 先看这样一个例子: 创建表 ...

  9. mysql小白系列_04 binlog(未完)

    mysql打开.查看.清理binlog 1.开启日志 log_bin=/var/lib/mysql/mysql-bin mysql> show variables like '%log_bin% ...

  10. flatbuffer介绍和用法

    介绍 flatbuffer是google发布的一个跨平台序列化框架具有如下特点 1.对序列化的数据不需要打包和拆包 2.内存和效率速度高,扩展灵活 3.代码依赖较少 4.强类型设计,编译期即可完成类型 ...