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 ...
随机推荐
- django “如何”系列10:如何管理静态文件
django开发者最关心的是web应用中的动态部分-视图函数和模板.但是明显,web应用还有其他需要注意的部分:静态文件(图片,css,javascript等等),那些都是渲染一个完整的页面需要的东西 ...
- linux命令(12):ping命令
1.ping网关:ping –b 192.168.1.1 2.ping指定次数:ping -c 10 192.168.1.100 3.时间间隔和次数限制的ping:ping -c 10 -i 0.5 ...
- redis之(八)redis的有序集合类型的命令
[一]增加元素 --->命令:ZADD key score member [score member] --->向有序集合放入一个分数为score的member元素 --->元素存在 ...
- WebDriver自动化测试工具(3)---PhantomJS的使用
PhantomJS是一个基于webkit的javascript API.它使用QtWebKit作为它核心浏览器的功能,使用webkit来编译解释执行JavaScript代码.任何你可以在基于webki ...
- 【剑指offer】面试题 2. 实现 Singleton 模式
面试题 2. 实现 Singleton 模式 题目:设计一个类,我们只能生成该类的一个实例. 单例模式:确保一个类只有一个实例,并提供了一个全局访问点. Java 实现 1.饿汉模式 //饿汉模式 p ...
- Centos Nodejs
设置Nodejs环境 第二节:Installing Node.js, PM2 and Yarn on CentOS https://www.youtube.com/watch?v=XCgCjasqEF ...
- urllib2模块、cookielib模块
urllib2模块 urllib模块和urllib模块类似,用来打开URL并从中获取数据.与urllib模块不同的是,urllib模块不仅可以使用urlopen() 函数还可以自定义Opener来访问 ...
- Linux命令之chgrp
chgrp [选项] … GROUP FILE … chgrp [选项] … --reference=RFILE FILE … chgrp命令是用来改变文件的组所有权.将改变每一个FILE的所属组为G ...
- (转) HA的几种方案
数据库HA 一般把数据库层面的HA,和应用层面HA分开考虑 数据库一般采用数据库产品提供的HA方案,比如Oracle的RAC,mysql的集群,mongodb的replica set等 无HA的运 ...
- Codeforces 551 D. GukiZ and Binary Operations
\(>Codeforces \space 551 D. GukiZ and Binary Operations<\) 题目大意 :给出 \(n, \ k\) 求有多少个长度为 \(n\) ...