java 432.所有O`one Data Structure.java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 432.所有O`one Data Structure.java相关的知识,希望对你有一定的参考价值。

public class AllOne {

    /** Initialize your data structure here. */
    
    private class Bucket{
    
        Bucket pre;
        Bucket next;
        int index;
        HashSet<String> keys;
        
        public Bucket(int index) {
            this.index = index;
            keys = new HashSet<String>();
        }
    }
    HashMap<String, Bucket> map;//used to store the mapping from key to the specific bucket 
    Bucket head = new Bucket(-1);
    Bucket tail = new Bucket(-1);
    public AllOne() {
        map = new HashMap<String, Bucket>();
        head.next = tail;
        tail.pre = head;
    }
    
    private void insertNextBucket(Bucket pre, int index) {
        //Insert the specific index Bucket after Bucket pre
        Bucket cur = new Bucket(index);
        pre.next.pre = cur;
        cur.next = pre.next;
        pre.next = cur;
        cur.pre = pre;
    }
    /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
    public void inc(String key) {
        if(!map.containsKey(key)) {
            if(head.next.index != 1) {
                insertNextBucket(head, 1);
            }
            head.next.keys.add(key);
            map.put(key, head.next);
        } else {
            Bucket pre = map.get(key);
            pre.keys.remove(key);
            if(pre.next.index != pre.index + 1) {
                insertNextBucket(pre, pre.index + 1);
            }
            pre.next.keys.add(key);
            map.put(key, pre.next);
            
            if(pre.keys.size() == 0){
                removeBucket(pre);
            }
        }
    }
    
    
    private void insertPreBucket(Bucket cur, int index) {
        //Insert the specific index Bucket before Bucket cur
        Bucket pre = new Bucket(index);
        
        cur.pre.next = pre;
        pre.pre = cur.pre;
        pre.next = cur;
        cur.pre = pre;

    }
    
    private void removeBucket(Bucket cur) {
        cur.pre.next = cur.next;
        cur.next.pre = cur.pre;
    }
    /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
    public void dec(String key) {
        
        if(!map.containsKey(key)) {
            return;
        }
        Bucket cur = map.get(key);
        cur.keys.remove(key);
        
        if (cur.index == 1) {
            map.remove(key);
        } else {
            if (cur.pre.index != cur.index - 1) {
                insertPreBucket(cur, cur.index - 1);
            }
            cur.pre.keys.add(key);
            map.put(key, cur.pre);
        }
        // if(cur.index != 1 && cur.pre.index != cur.index - 1) {
        //     insertPreBucket(cur, cur.index - 1);
        //     cur.pre.keys.add(key);
        //     map.put(key, cur.pre);
        // } else if (cur.index != 1) {
        //     cur.pre.keys.add(key);
        //     map.put(key, cur.pre);
        // } else if(cur.index == 1) {
        //     map.remove(key);
        // }
        
        if(cur.keys.size() == 0){
            removeBucket(cur);
        }
    }
    
    /** Returns one of the keys with maximal value. */
    public String getMaxKey() {
        
        //return tail.pre.keys.iterator().next();
        
        return tail.pre == head ? "" : tail.pre.keys.iterator().next();
    }
    
    /** Returns one of the keys with Minimal value. */
    public String getMinKey() {
        //return head.next.keys.iterator().next();
        return head.next == tail ? "" : head.next.keys.iterator().next();
    }
}

/**
 * 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();
 */
 
 /*
 ["AllOne","getMaxKey","getMinKey"]
[[],[],[]]
["AllOne","inc","inc","getMaxKey","getMinKey","inc","getMaxKey","getMinKey"]
[[],["hello"],["hello"],[],[],["leet"],[],[]]
["AllOne","dec","getMaxKey"]
[[],["hello"],[]]
["AllOne","inc","inc","inc","inc","inc","dec","dec","getMaxKey","getMinKey"]
[[],["a"],["b"],["b"],["b"],["b"],["b"],["b"],[],[]]
["AllOne","inc","inc","inc","inc","inc","inc","getMaxKey","inc","dec","getMaxKey","dec","inc","getMaxKey","inc","inc","dec","dec","dec","dec","getMaxKey","inc","inc","inc","inc","inc","inc","getMaxKey","getMinKey"]
[[],["hello"],["world"],["leet"],["code"],["DS"],["leet"],[],["DS"],["leet"],[],["DS"],["hello"],[],["hello"],["hello"],["world"],["leet"],["code"],["DS"],[],["new"],["new"],["new"],["new"],["new"],["new"],[],[]]
["AllOne","inc","inc","inc","inc","inc","dec","getMaxKey","getMinKey","inc","inc","inc","getMaxKey","getMinKey","inc","inc","getMinKey"]
[[],["hello"],["hello"],["world"],["world"],["hello"],["world"],[],[],["world"],["world"],["leet"],[],[],["leet"],["leet"],[]]

 */

以上是关于java 432.所有O`one Data Structure.java的主要内容,如果未能解决你的问题,请参考以下文章

java 432.所有O`one Data Structure.java

java 432.所有O`one Data Structure.java

java 432.所有O`one Data Structure.java

java 432.所有O`one Data Structure.java

Leetcode: All O`one Data Structure

[LeetCode] All O`one Data Structure 全O的数据结构