all-oone-data-structure(好)

哈哈,我用了HashMap, 双向链表,还有了HashSet来保存key的集合。

现在这道题目还只有 9.3%的AC率,难度为HardTotal 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();
     */
时间: 2024-12-24 01:12:38

all-oone-data-structure(好)的相关文章

What is “passive data structure” in Android/Java?

From the Android developer web link: http://developer.android.com/reference/android/content/Intent.html, you can find that it says "It (Intent) is basically a passive data structure holding an abstract description of an action to be performed."

[LeetCode] 211. Add and Search Word - Data structure design Java

题目: Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one le

170. Two Sum III - Data structure design

Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value. For example, add(1); ad

LeetCode OJ:Add and Search Word - Data structure design(增加以及搜索单词)

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

[leedcode 211] Add and Search Word - Data structure design

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

uva 11995 - I Can Guess the Data Structure!

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=3146&mosmsg=Submission+received+with+ID+14262472 I Can Guess the Data Structure! There is a bag-like data structure, supporti

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

Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with value 1. Or increments an existing key by 1. Key is guaranteed to be a non-empty string. Dec(Key) - If Key's value is 1, remove it from the data structu

hdu-5929 Basic Data Structure(双端队列+模拟)

题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 207    Accepted Submission(s): 41 Problem Description Mr. Frog learned a basic data structure recently, which is called

211. Add and Search Word - Data structure design

就是trie 1 public class WordDictionary { 2 public class TrieNode { 3 public TrieNode[] child; 4 public char curChar; 5 public boolean isLeaf; 6 7 public TrieNode() { 8 child = new TrieNode[26]; 9 isLeaf = false; 10 } 11 } 12 13 TrieNode root; 14 15 pub

(Data structure)Implement Trie And Add and Search Word

Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs are consist of lowercase letters a-z solution: class TrieNode { // Initialize your data structure here. boolean isEnd; Trie