all-oone-data-structure(好)
哈哈,我用了HashMap, 双向链表,还有了HashSet来保存key的集合。
现在这道题目还只有 9.3%的AC率,难度为Hard
Total Accepted: 9
Total Submissions: 97
Difficulty: Hard
我也把解法发到了Discuss版:
https://discuss.leetcode.com/topic/63559/my-accepted-java-solution-with-hashmap-and-double-linked-list
https://leetcode.com/problems/all-oone-data-structure/ class AllOne { // Hashmap for get Node to operate inc and dec
// LinkNode to maintain order
// HashSet to maintain key with certain value private class LinkNode {
public int val; // maintain current value
LinkNode small; // maintain link to smaller node
LinkNode big; // maintain link to bigger node
Set<String> keySet; // maintain keys with current value LinkNode(String key, int v) {
keySet = new HashSet<>();
keySet.add(key);
val = v;
}
} private LinkNode maxNode;
private LinkNode minNode;
Map<String, LinkNode> hmap; /** Initialize your data structure here. */
public AllOne() {
hmap = new HashMap<>();
maxNode = null;
minNode = null;
} /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
public void inc(String key) {
LinkNode lknd; if (hmap.containsKey(key)) {
lknd = hmap.get(key);
lknd.keySet.remove(key); if (lknd.big != null) {
if (lknd.big.val == lknd.val+1) {
lknd.big.keySet.add(key);
hmap.put(key, lknd.big);
}
else {
LinkNode lbig = new LinkNode(key, lknd.val+1);
lbig.small = lknd;
lbig.big = lknd.big;
lknd.big = lbig;
lbig.big.small = lbig;
hmap.put(key, lbig);
}
}
else {
LinkNode lbig = new LinkNode(key, lknd.val+1);
lbig.small = lknd;
lknd.big = lbig;
maxNode = lbig;
hmap.put(key, lbig);
} // remove original node if necessary
if (lknd.keySet.isEmpty()) {
// for minNode
if (minNode == lknd) {
minNode = lknd.big;
}
else {
lknd.small.big = lknd.big;
}
// for maxNode
if (maxNode == lknd) {
maxNode = lknd.small;
}
else {
lknd.big.small =lknd.small;
}
}
}
else {
if (minNode == null) {
// all list is null
lknd = new LinkNode(key, 1);
minNode = lknd;
maxNode = lknd;
}
else {
if (minNode.val != 1) {
lknd = new LinkNode(key, 1);
lknd.big = minNode;
minNode.small = lknd;
minNode = lknd;
}
else {
minNode.keySet.add(key);
lknd = minNode;
}
}
hmap.put(key, lknd);
}
} /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
public void dec(String key) {
LinkNode lknd; if (hmap.containsKey(key)) {
lknd = hmap.get(key);
lknd.keySet.remove(key); if (lknd.val == 1) {
hmap.remove(key);
}
else {
if (lknd.small != null) {
if (lknd.small.val == lknd.val - 1) {
lknd.small.keySet.add(key);
hmap.put(key, lknd.small);
}
else {
LinkNode lsmall = new LinkNode(key, lknd.val - 1);
lsmall.big = lknd;
lsmall.small = lknd.small;
lknd.small = lsmall;
lsmall.small.big = lsmall;
hmap.put(key, lsmall);
}
}
else {
LinkNode lsmall = new LinkNode(key, lknd.val - 1);
lknd.small = lsmall;
lsmall.big = lknd;
minNode = lsmall;
hmap.put(key, lsmall);
}
} // remove original node if necessary
if (lknd.keySet.isEmpty()) {
// for minNode
if (minNode == lknd) {
minNode = lknd.big;
}
else {
lknd.small.big = lknd.big;
}
// for maxNode
if (maxNode == lknd) {
maxNode = lknd.small;
}
else {
lknd.big.small =lknd.small;
}
} }
} /** Returns one of the keys with maximal value. */
public String getMaxKey() {
if (maxNode == null) {
return "";
}
Iterator<String> iter = maxNode.keySet.iterator();
return iter.next();
} /** Returns one of the keys with Minimal value. */
public String getMinKey() {
if (minNode == null) {
return "";
}
Iterator<String> iter = minNode.keySet.iterator();
return iter.next();
} public void printAll() {
LinkNode tmp = maxNode;
System.out.println("Start print");
if (tmp != null) {
System.out.printf("Node val: %d\n", tmp.val);
Iterator<String> iter = tmp.keySet.iterator();
while (iter.hasNext()) {
System.out.printf("key: %s,", iter.next());
}
System.out.println();
tmp = tmp.small;
}
if (minNode != null) {
System.out.printf("Min Node val: %d\n", minNode.val);
Iterator<String> iter = minNode.keySet.iterator();
while (iter.hasNext()) {
System.out.printf("key: %s,", iter.next());
}
System.out.println();
}
System.out.println("End print");
}
} /**
* 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();
*/
all-oone-data-structure(好)的更多相关文章
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- Finger Trees: A Simple General-purpose Data Structure
http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...
- Mesh Data Structure in OpenCascade
Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...
- ✡ leetcode 170. Two Sum III - Data structure design 设计two sum模式 --------- java
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- leetcode Add and Search Word - Data structure design
我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...
- 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 ...
- HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...
- LeetCode Two Sum III - Data structure design
原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...
随机推荐
- 怎么制作CHM格式电子书
CHM格式的帮助文件相信大家都不陌生,CHM文件形式多样,使用方便,深受大家喜爱. 今天给大家介绍一种把文本文件转换为CHM格式电子书的方法. 前期准备过程 1 下载QuickCHM v2.6文件 去 ...
- 前端读者 | 从一行代码里面学点JavaScript
本文来自 @张小俊128:链接:http://www.html-js.com/article/A-day-to-learn-from-a-line-of-code-inside-the-JavaScr ...
- gulp-connect插件浏览器实时同步刷新
1.在站点路径里打开cmd控制台. 输入:cnpm install gulp-connect --save-dev 2.编辑gulpfile.js 3.控制台执行gulp任务 输入gulp serve ...
- Ace Admin 学习笔记
1. jqGrid 提交编辑数据,控制台报:Synchronous XMLHttpRequest on the main thread... jqGrid的选项设置async: true选项: aj ...
- 轻松学习LINUX系列教程推出
本系列多媒体教程已完成的博文: 1.轻松学习Linux之入门篇 http://chenguang.blog.51cto.com/350944/449214 2.轻松学习Linux之本地安装系统 (分区 ...
- LeetCode 461 汉明距离/LintCode 365 统计二进制中1的个数
LeetCode 461. 汉明距离 or LintCode 365. 二进制中有多少个1 题目一:LeetCode 461. 汉明距离 LeetCode 461.明距离(Hamming Distan ...
- 选择排序(SelectionSort)
http://blog.csdn.net/magicharvey/article/details/10274765 算法描述 选择排序是一种不稳定排序.选择排序每次交换一对元素,它们当中至少有一个将被 ...
- python 正则表达式匹配中文(转)
网上的一篇文章,做了整理,作者已无从考证,谢谢了 s=""" en: Regular expression is a powerful tool for manipula ...
- 51使用while进行延时的问题
上周写了一个简单的IO口翻转的程序,但是很奇怪,在FPGA板子上怎么也跑不起来. 后面发现问题可能出在延时函数上. void Delay(unsigned char t) { while(--t); ...
- hdu1006 Tick and Tick (数学题 借鉴了大神的博客)
先缩短一半的时间:早上的12个小时和下午的12小时对时钟是一样的,因为时钟12小时与0小时的三针位置相同.接着就是了解到每次所有的针从有重合到再次有重合至多有一段连续的段符合三针分离度大于n.所以只要 ...