哈哈,我用了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(好)的更多相关文章

  1. [LeetCode] All O`one Data Structure 全O(1)的数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  2. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  3. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

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

  5. Mesh Data Structure in OpenCascade

    Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...

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

  7. leetcode Add and Search Word - Data structure design

    我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...

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

  9. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...

  10. LeetCode Two Sum III - Data structure design

    原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...

随机推荐

  1. django-1366, "Incorrect string value: '\\xE6\\x88\\x9A\\xE4\\xBC\\x9F...'

    今天把之前的一些代码转移到另外一台电脑的时候, python manage.py syncdb 的时候报了 (1366, "Incorrect string value: '\\xE6\\x ...

  2. 取消div,a等标签点击效果

    当标签被设置onclick事件之后,在有些手机浏览器中,点击这些标签,会有点击变色效果.想要取消点击变色效果. 添加:div{-webkit-tap-highlight-color:rgba(0,0, ...

  3. Zookeeper之Curator(1)客户端对节点的一些监控事件的api使用

    <一>节点改变事件的监听 public class CauratorClientTest { //链接地址 private static String zkhost="172.1 ...

  4. JavaScript如何判断变量是数组还是对象

    编辑 方法一:通过判断变量的类型,并且变量的length属性(除了有一种例外是arguments对象–当给函数传参时数据存储的地方) var arr=[2,3,4]; var obj={"n ...

  5. Flexigrid默认是可以选择多行

    1.Flexigrid默认是可以选择多行,那么如何设置其只能选一行呢?今天看了看Flexigrid的源码,发现有个属性可以控制: $(this).click(function (e) { var ob ...

  6. C/C++宏的用法

    今天看caffe源码的时候看到了很多宏定义的内容,苦于代码基础薄弱,无法全部理解,故在网上搜得此篇好文,转载一发附原文地址:http://blog.csdn.net/hanchaoman/articl ...

  7. HDU 4607.Park Visit-树的直径(BFS版)+结论公式(乱推公式)-备忘(加油!)

    Park Visit Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. hihoCoder #1871 : Heshen's Account Book-字符串暴力模拟 自闭(getline()函数) (ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction B) 2018 ICPC 北京区域赛现场赛B

    P2 : Heshen's Account Book Time Limit:1000ms Case Time Limit:1000ms Memory Limit:512MB Description H ...

  9. NestedScrollView

    参考文章: Android滑动到顶部悬停 NestedScrollView的使用 效果图: 实现步骤: 将需要悬浮的layout放到CollapsingToolbarLayout之外,AppBarLa ...

  10. 【Bzoj4555】【Luogu P4091】求和(NTT)

    题面 Bzoj Luogu 题解 先来颓柿子 $$ \sum_{i=0}^n\sum_{j=0}^iS(i,j)2^jj! \\ =\sum_{j=0}^n2^jj!\sum_{i=0}^nS(i,j ...