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 ...
随机推荐
- dumpsys-package
dumpsys-package ams和pms是android系统最重要的系统服务,本文解析dumpsys package命令,看哪些PMS相关的系统信息,数据结构是运行时可以查看的. 命令提示 co ...
- c++(qt)程序员微信群
关注微信公众号"程序员成长日志",回复关键字"c++"扫码进群 本群主要为大家解决工作中遇到的问题遇到的问题发到群里大家集思广益平时可以瞎扯不定期红包
- 一阶RC高通滤波器详解(仿真+matlab+C语言实现)
文章目录 预备知识 关于电容 HPF的推导 simulink 仿真 simulink 运行结果 matlab 实现 matlab 运行结果 C语言实现 如果本文帮到了你,帮忙点个赞: 如果本文帮到了你 ...
- [工具]微软的学习平台Microsoft Learn很好用,推荐一下
1. 什么是Microsoft Learn Microsoft Learn是微软这两年大力推广的全新学习平台,可提供 Microsoft 产品交互式学习体验.基本上无需登录即可使用,但登录后可以使用更 ...
- Cassandra数据建模
1. 概述 Apache Cassandra将数据存储在表中,每个表都由行和列组成.CQL(Cassandra查询语言)用于查询存储在表中的数据.Apache Cassandra数据模型基于查询并针 ...
- LeetCode链表专题
链表 套路总结 1.多个指针 移动 2.虚假链表头:凡是有可能删除头节点的都创建一个虚拟头节点,代码可以少一些判断(需要用到首部前一个元素的时候就加虚拟头指针) 3.快慢指针 如leetcode160 ...
- java ->包的声明与访问
包的声明与访问 包的概念 java的包,其实就是我们电脑系统中的文件夹,包里存放的是类文件. 当类文件很多的时候,通常我们会采用多个包进行存放管理他们,这种方式称为分包管理. 在项目中,我们将相同功能 ...
- (Redis基础教程之六)如何使用Redis中的List
如何在ubuntu18.04上安装和保护redis 如何连接到Redis数据库 如何管理Redis数据库和Keys 如何在Redis中管理副本和客户端 如何在Redis中管理字符串 如何在Redis中 ...
- codis原理及部署_01
一.codis介绍 Codis是一个分布式Redis解决方案,对于上层的应用来说,连接到Codis Proxy和连接原生的RedisServer没有明显的区别,有部分命令不支持 Codis底层会处理请 ...
- 【python爬虫】解决歌荒,下歌利器
python下载图片,mp3,想必很多人都早已耳闻,今天给大家来点不一样的, 让你下载高逼格高品质,带进度条,实时显示下载速度 详见源码:https://www.kesci.com/home/proj ...