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 ...
随机推荐
- P2725 邮票 Stamps(完全背包+限制填充数)
题目链接:https://www.luogu.org/problem/show?pid=2725 题目大意:给一组 N 枚邮票的面值集合(如,{1 分,3 分})和一个上限 K —— 表示信封上能够贴 ...
- [译]怎样用HTML5 Canvas制作一个简单的游戏
这是我翻译自LostDecadeGames主页的一篇文章,原文地址:How To Make A Simple HTML5 Canvas Game. 下面是正文: 自从我制作了一些HTML5游戏(例如C ...
- es6 map数据类型,要比set还很多
首先它支持多数据存储,具有增删查功能 set()设置 get()获取; has()查找; delete('obj')删除指定:clear()全部删除 size长度 let json={ name:&q ...
- 五十八 数据库访问使用SQLite
SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成. Python就 ...
- PHP 边执行边输出
<?php for ($i = 1; $i <= 5; $i++) { print "#$i 完毕<hr>"; sleep(1); print str_pa ...
- CodeForces 738C Road to Cinema
二分答案. 油量越多,显然通过的时间越少.可以二分找到最小的油量,可以在$t$时间内到达电影院. 一个油箱容量为$v$的车通过长度为$L$的路程需要的最小时间为$max(L,3*L-v)$.计算过程如 ...
- 洛谷P1120 小木棍 [搜索]
题目传送门 题目描述乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它们的长度. 给出每段小木棍 ...
- go chapter 2 - read file(yaml)
func main() { data, err := ioutil.ReadFile("D:/test/widua.go") if err != nil { fmt.Println ...
- 设计模式-组合模式(Composite Pattern)
本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 前置技能:认识数据结构中的树形结构. 组合模式简介 组合模式是将对象组合成树形结构以表示“部分- ...
- SqlHelper——只因为在人群中多看了你一眼
对SQLHelper,还是有一点陌生的,但是大多数人都在使用,我就有一种想了解并使用的意愿,于是查了很多资料,发现一片不错的博客,放在下面,作为自己或读者使用的材料. 一.SqlHelper 出场 不 ...