前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 432. 全 O(1) 的数据结构(设计题)*

LeetCode 432. 全 O(1) 的数据结构(设计题)*

作者头像
Michael阿明
发布2021-02-19 09:51:00
4290
发布2021-02-19 09:51:00
举报
文章被收录于专栏:Michael阿明学习之路

文章目录

1. 题目

请你实现一个数据结构支持以下操作:

  • Inc(key) - 插入一个新的值为 1 的 key。 或者使一个存在的 key 增加一,保证 key 不为空字符串。
  • Dec(key) - 如果这个 key 的值是 1,那么把他从数据结构中移除掉。 否则使一个存在的 key 值减一。 如果这个 key 不存在,这个函数不做任何事情。key 保证不为空字符串。
  • GetMaxKey() - 返回 key 中值最大的任意一个。 如果没有元素存在,返回一个空字符串"" 。
  • GetMinKey() - 返回 key 中值最小的任意一个。 如果没有元素存在,返回一个空字符串""。

挑战: 你能够以 O(1) 的时间复杂度实现所有操作吗?

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/all-oone-data-structure 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

参考大佬的题解

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
class node
{
public:
    int val;
    unordered_set<string> s;
    node(int v)
    {
        val = v;
    }
};
class AllOne {
    unordered_map<string, list<node>::iterator> m;
    list<node> l;
public:
    /** Initialize your data structure here. */
    AllOne() {

    }
    
    /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
    void inc(string key) {
        auto it = m.find(key);
        if(it == m.end())
        {
            if(l.empty() || l.front().val > 1)
                l.push_front(node(1));//没有val为1的,新建
            l.begin()->s.insert(key);
            m[key] = l.begin();
        }
        else
        {
            auto iter = it->second;//迭代器位置
            int num = iter->val;//当前数字
            iter->s.erase(key);//原位置处删除
            auto olditer = iter;
            auto newiter = ++iter;
            num++;
            if(newiter != l.end() && newiter->val == num)
            {	//新位置数字刚好是+1以后的
                newiter->s.insert(key);
                m[key] = newiter;
            }
            else
            {	//新开辟节点
                auto temp = l.insert(newiter,node(num));//新节点之前插入
                temp->s.insert(key);//返回的当前节点存入字符串
                m[key] = temp;
            }
            if(olditer->s.empty())
                l.erase(olditer);//原来节点为空要删除
        }
    }
    
    /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
    void dec(string key) {
        auto it = m.find(key);
        if(it == m.end()) return;
        auto iter = it->second;
        int num = iter->val;
        iter->s.erase(key);
        auto olditer = iter;
        auto newiter = --iter;
        num--;
        if(num == 0)
            m.erase(key);
        else if(olditer != l.begin() && newiter->val == num)
        {	//前面还有节点,且数字吻合
            newiter->s.insert(key);
            m[key] = newiter;
        }
        else
        {	//前面数字不符合,在老节点前插入
            auto temp = l.insert(olditer,node(num));
            temp->s.insert(key);
            m[key] = temp;
        }
        if(olditer->s.empty())
            l.erase(olditer);
    }
    
    /** Returns one of the keys with maximal value. */
    string getMaxKey() {
        if(l.empty()) return "";
        return *(l.back().s.begin());
    }
    
    /** Returns one of the keys with Minimal value. */
    string getMinKey() {
        if(l.empty()) return "";
            return *(l.front().s.begin());
    }
};

100 ms 24.9 MB

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/07/16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 1. 题目
  • 2. 解题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档