leetcode每日一题 设计一个验证系统

该文章描述了一个用C++和Python编写的验证系统,系统使用哈希表存储tokenId及其有效期。generate方法生成令牌,renew方法根据当前时间重置令牌的有效性,countUnexpiredTokens方法计算未过期令牌的数量。
摘要由CSDN通过智能技术生成

设计一个验证系统

  1. 题意

  2. 分析

    • 给定tokenId,currentTime以及timeToLive,我们定义一个字典,存储的形式为dict[tokenId] = currentTime + timeToLive.
    • 在renew函数中,判断一下tokenId是否在字典的键中以及对应的值是否小于currentTime,如果都成立,则重置验证码,即dict[tokenId] = currentTime + timeToLive. 否则什么也不做。
    • 在countUnexpiredTokens函数中,定义一个变量res = 0, 依次遍历字典,如果字典中的值小于给定的currentTime,则res ++, 否则什么也不做。
    • 最后返回res即可。
  3. 代码:

    • C++代码
    class AuthenticationManager {
    public:
        int timeToLive;
        unordered_map<string, int> mp;
        
        AuthenticationManager(int timeToLive) {
            this->timeToLive = timeToLive;
        }
        void generate(string tokenId, int currentTime) {
            mp[tokenId] = currentTime + timeToLive;
        }
        void renew(string tokenId, int currentTime) {
            if (mp.count(tokenId) && mp[tokenId] > currentTime){
                mp[tokenId] = currentTime + timeToLive;
            }
        }
        int countUnexpiredTokens(int currentTime) {
            int res = 0;
            for(auto &[_, time]: mp){
                if (time > currentTime) {
                    res ++;
                }
            }
            return res;
        }
    };
    
    /**
    * Your AuthenticationManager object will be instantiated and called as such:
    * AuthenticationManager* obj = new AuthenticationManager(timeToLive);
    * obj->generate(tokenId,currentTime);
    * obj->renew(tokenId,currentTime);
    * int param_3 = obj->countUnexpiredTokens(currentTime);
    */
    
    • python代码:
    class AuthenticationManager:
        def __init__(self, timeToLive: int):
            self.timeToLive = timeToLive
            self.mp = dict()
        def generate(self, tokenId: str, currentTime: int) -> None:
            self.mp[tokenId] = currentTime + self.timeToLive
        def renew(self, tokenId: str, currentTime: int) -> None:
            if tokenId in self.mp and self.mp[tokenId] > currentTime: self.mp[tokenId] = currentTime + self.timeToLive
        def countUnexpiredTokens(self, currentTime: int) -> int:
            res = 0
            for _, time in self.mp.items():
                if time > currentTime: res += 1
            return res
    # Your AuthenticationManager object will be instantiated and called as such:
    # obj = AuthenticationManager(timeToLive)
    # obj.generate(tokenId,currentTime)
    # obj.renew(tokenId,currentTime)
    # param_3 = obj.countUnexpiredTokens(currentTime)
    
  4. 分析

    • 使用哈希表较为方便;
      .countUnexpiredTokens(currentTime)
    
    
  5. 分析

    • 使用哈希表较为方便;
    • 模拟题。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KevinHQK

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值