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 ...
随机推荐
- hdu 2883(构图+最大流+压缩区间)
kebab Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 利用WINDOWS活动目录提供LDAP的方案
Windows Server 2008 R2 活动目录服务安装 http://blog.sina.com.cn/s/blog_622de9390100kgv3.html WINDOWS 2008 域控 ...
- JQuery 实现 锚点跳转
$('.nav-jump').click(function() { $('html,body').animate( { scrollTop:$($.attr(this, 'href')).offset ...
- 查看linux版本及lsb_release安装及一些想法
https://blog.csdn.net/darkdragonking/article/details/61194308
- W3School WebService教程
http://www.w3school.com.cn/webservices/index.asp
- JavaScript中字符串分割函数split用法实例
这篇文章主要介绍了JavaScript中字符串分割函数split用法,实例分析了javascript中split函数操作字符串的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了JavaSc ...
- react native android 应用状态(前端或后台)的判断
当Android应用程序被暂时放到了后台,或者又重新回到前台,是否有相应的事件可以处理到? 例如,当你的应用暂时放到了后台,是否应该做出一些操作,暂时保存界面上的数据? 可以参考:https://gi ...
- web.config 数据库连接
方法一:connectionsStrings 首先配置web.config文件 <configurations> <connectionStrings> <add nam ...
- bootstrap插件学习-bootstrap.tab.js(读码)
先看bootstrap-tab.js的结构 var Tab = function ( element ) {} //构造器 Tab.prototype ={} //构造器的原型 $.fn.tab = ...
- 【SQL】ORACLE在sqlplus中使用spool方式生成建表语句
在实际生产中有时我们需要将一张表的数据导入到另外一张表,如果有PLSQL,我们可以通过PLSQL工具将数据导出为sql脚本,然后再在另外一个数据库中执行这个脚本.但有时在实际生产中我们没有PLSQL这 ...