[leetcode] 432. 全 O(1) 的数据结构 | STL 双向链表&哈希

题目链接
在这里插入图片描述

之前没有做过这种类型的题目,看到的时候一脸蒙圈
看了官方题解之后,了解到这个是双向链表
然后来写一下题解:
我们可以维护一个链表,这个链表是一个双向的,把这个链表维护成从头节点到尾节点是单调递增的,然后我们就可以很好的通过头尾返回出现次数最多(尾部)和出现次数最小的字符串(头部)
在这个链表里面,我们存入两个部分,用 p a i r pair pair做在一起,第一部分是存放 s t r i n g string string u n o r d e r e d s e t unordered_set unorderedset容器 k e y s keys keys,用来放置字符串,然后我们用第二部分来标识这个字符串出现的次数 c o u n t count count
对于一个给定的字符串,如果不用遍历的防止获取他的位置,那还能怎么做呢?
:用一个哈希表 n o d e s nodes nodes,用来标识每个字符串的位置,我们这里可以用STL中的 u n o r d e r e d m a p < > unordered_map<> unorderedmap<>,键就是字符串string,而值是列表的迭代器, l i s t < p > : : i t e r a t o r list<p>::iterator list<p>::iterator

  1. 然后对于 i n c inc inc操作来说:
    如果当前字符串不在链表中,并且链表为空或者是头节点的字符串出现的次数大于1的时候,先插入一个 c o u n t = 1 count=1 count=1的新节点至链表的头部,然后将字符串插画如到头结点的 k e y s keys keys
    如果说当前字符串key在链表当中,并且所在链表节点位置为 c u r cur cur,如果说 c u r . n e x t cur.next cur.next为空或者是 c u r . n e x t . c o u n t > c u r . c o u n t + 1 cur.next.count>cur.count+1 cur.next.count>cur.count+1,那么说我们插入一个 c o u n t = c u r . c o u n t + 1 count=cur.count+1 count=cur.count+1的新节点至 c u r cur cur之后,然后将 k e y key key插入到 c u r . n e x t . k e y s cur.next.keys cur.next.keys中,最后再将 k e y key key c u r . k e y s cur.keys cur.keys中移除,如果说一处之后 c u r . k e y s cur.keys cur.keys为空,那么就将 c u r cur cur从链表中移除掉
    然后更新 n o d e s nodes nodes k e y key key所处的节点
  2. 对于 d e c dec dec
    如果说 k e y key key只出现了一次,那么说 − 1 -1 1之后就变成了0,就要被移除
    如果说出现了不止一次,那么就找到 k e y key key所在节点 c u r cur cur,如果说 c u r . p r e v cur.prev cur.prev为空或者是 c u r . p r e v . c o u n t < c u r . c o u n t − 1 cur.prev.count<cur.count-1 cur.prev.count<cur.count1,则先插入一个 c o u n t = c u r . c o u n t − 1 count = cur.count-1 count=cur.count1的新节点到 c u r cur cur之前,然后将 k e y key key插入到 c u r . p r e v . k e y s cur.prev.keys cur.prev.keys中。然后最后更新我们的哈希表 n o d e s nodes nodes中的当前字符串所处的节点,一共下次使用
    最后,我们将当前字符串从 c u r . k e y s cur.keys cur.keys中移除掉,如果说 c u r . k e y s cur.keys cur.keys为空了,就直接将 c u r cur cur删掉即可
    Code:
class AllOne {
    typedef pair<unordered_set<string>,int> p;
    list<p> lst;
    unordered_map<string,list<p>::iterator> nodes;
public:
    AllOne() {    }
    
    void inc(string key) {
        if(nodes.count(key)) {
            auto cur = nodes[key];
            auto nex = next(cur);
            if(nex == lst.end() || nex->second > cur->second+1) {
                unordered_set<string>s({key});
                nodes[key] = lst.emplace(nex,s,cur->second+1);
            } else {
                nex->first.emplace(key);
                nodes[key] = nex;
            }
            cur->first.erase(key);
            if(cur->first.empty()) lst.erase(cur);
        } else {// not in list
            if(lst.empty() || lst.begin()->second > 1) {
                unordered_set<string>s({key});
                lst.emplace_front(s,1);
            } else {
                lst.begin()->first.emplace(key);
            }
            nodes[key] = lst.begin();
        }
    }
    
    void dec(string key) {
        auto cur = nodes[key];
        if(cur->second == 1) {
            nodes.erase(key);
        } else {
            auto pre = prev(cur);
            if(cur == lst.begin() || pre->second < cur->second - 1) {
                unordered_set<string> s({key});
                nodes[key] = lst.emplace(cur,s,cur->second - 1);
            } else {
                pre->first.emplace(key);
                nodes[key] = pre;
            }
        }
        cur->first.erase(key);
        if(cur->first.empty()) lst.erase(cur);
    }
    
    string getMaxKey() {
        if(lst.empty()) return "";
        else return *lst.rbegin()->first.begin();
    }
    
    string getMinKey() {
        if(lst.empty()) return "";
        else return *lst.begin()->first.begin();
    }
};

/**
 * 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();
 */
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值