首页 > 其他分享 >力扣---1797. 设计一个验证系统

力扣---1797. 设计一个验证系统

时间:2023-02-09 22:11:07浏览次数:42  
标签:力扣 aaa currentTime int tokenId 1797 验证码 --- timeToLive

你需要设计一个包含验证码的验证系统。每一次验证中,用户会收到一个新的验证码,这个验证码在 currentTime 时刻之后 timeToLive 秒过期。如果验证码被更新了,那么它会在 currentTime (可能与之前的 currentTime 不同)时刻延长 timeToLive 秒。

请你实现 AuthenticationManager 类:

    AuthenticationManager(int timeToLive) 构造 AuthenticationManager 并设置 timeToLive 参数。
    generate(string tokenId, int currentTime) 给定 tokenId ,在当前时间 currentTime 生成一个新的验证码。
    renew(string tokenId, int currentTime) 将给定 tokenId 且 未过期 的验证码在 currentTime 时刻更新。如果给定 tokenId 对应的验证码不存在或已过期,请你忽略该操作,不会有任何更新操作发生。
    countUnexpiredTokens(int currentTime) 请返回在给定 currentTime 时刻,未过期 的验证码数目。

如果一个验证码在时刻 t 过期,且另一个操作恰好在时刻 t 发生(renew 或者 countUnexpiredTokens 操作),过期事件 优先于 其他操作。

 

示例 1:

输入:
["AuthenticationManager", "renew", "generate", "countUnexpiredTokens", "generate", "renew", "renew", "countUnexpiredTokens"]
[[5], ["aaa", 1], ["aaa", 2], [6], ["bbb", 7], ["aaa", 8], ["bbb", 10], [15]]
输出:
[null, null, null, 1, null, null, null, 0]

解释:
AuthenticationManager authenticationManager = new AuthenticationManager(5); // 构造 AuthenticationManager ,设置 timeToLive = 5 秒。
authenticationManager.renew("aaa", 1); // 时刻 1 时,没有验证码的 tokenId 为 "aaa" ,没有验证码被更新。
authenticationManager.generate("aaa", 2); // 时刻 2 时,生成一个 tokenId 为 "aaa" 的新验证码。
authenticationManager.countUnexpiredTokens(6); // 时刻 6 时,只有 tokenId 为 "aaa" 的验证码未过期,所以返回 1 。
authenticationManager.generate("bbb", 7); // 时刻 7 时,生成一个 tokenId 为 "bbb" 的新验证码。
authenticationManager.renew("aaa", 8); // tokenId 为 "aaa" 的验证码在时刻 7 过期,且 8 >= 7 ,所以时刻 8 的renew 操作被忽略,没有验证码被更新。
authenticationManager.renew("bbb", 10); // tokenId 为 "bbb" 的验证码在时刻 10 没有过期,所以 renew 操作会执行,该 token 将在时刻 15 过期。
authenticationManager.countUnexpiredTokens(15); // tokenId 为 "bbb" 的验证码在时刻 15 过期,tokenId 为 "aaa" 的验证码在时刻 7 过期,所有验证码均已过期,所以返回 0 。

 

提示:

    1 <= timeToLive <= 108
    1 <= currentTime <= 108
    1 <= tokenId.length <= 5
    tokenId 只包含小写英文字母。
    所有 generate 函数的调用都会包含独一无二的 tokenId 值。
    所有函数调用中,currentTime 的值 严格递增 。
    所有函数的调用次数总共不超过 2000 次。

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

 

主要是题目难懂,具体逻辑没啥难点。

class AuthenticationManager {
    private static int timeToLive;
    private Map<String, Integer> map = new HashMap<>();

    public AuthenticationManager(int timeToLive) {
        this.timeToLive = timeToLive;
    }

    public void generate(String tokenId, int currentTime) {
        map.put(tokenId, currentTime + timeToLive);
    }

    public void renew(String tokenId, int currentTime) {
        if (map.containsKey(tokenId) && map.get(tokenId) > currentTime) {
            map.put(tokenId, currentTime + timeToLive);
        }
    }

    public int countUnexpiredTokens(int currentTime) {
        int count = 0;
        for (int time : map.values()) {
            if (time > currentTime) {
                count ++;
            }
        }
        return count;
    }
}

 

 暂时这样吧,以后理解再深一些写优化。

标签:力扣,aaa,currentTime,int,tokenId,1797,验证码,---,timeToLive
From: https://www.cnblogs.com/allWu/p/17107340.html

相关文章

  • 接口文档 jwt介绍和原理 drf-jwt快速使用 定制返回格式
    今日内容总结接口文档#接口文档如何编写 -1使用word,md编写接口文档 -2使用第三方平台编写我们的接口文档(非常多)--->收费 -https://www.showdoc.com.......
  • C语言--初识函数
     ......
  • 正则表达式-(6)转义
    一、转义字符转义序列通常有两种功能。第一种功能是编码无法用字母表直接表示的特殊数据。第二种功能是用于表示无法直接键盘录入的字符(如回车符)。 二、正则中元字符......
  • DRF - coreapi自动生成接口文档、JWT
    目录1接口文档1.什么是接口文档2.接口文档的编写方式3.使用coreapi自动生成接口文档步骤(1)安装coreapi(2)配置路由(3)文档说明文字:在视图类,方法上写注释即可(4)配置文件中配置自......
  • 设计模式(七)----创建型模式之建造者模式
    1、概述将一个复杂对象的构建与表示分离,使得同样的构建过程可以创建不同的表示。分离了部件的构造(由Builder来负责)和装配(由Director负责)。从而可以构造出复杂的......
  • Java-Integer好大一坑,一不小心就掉进去了
    遛马少年,一个代码写的很6的程序员,专注于技术干货分享最近,在处理线上bug的时候,发现了一个奇怪的现象业务代码大概是这样的publicstaticbooleandoSth(Integerx,Int......
  • 05-Verilog基础语法
    Verilog基础语法MixedModel(混合设计模型)SystemTasks(系统任务,系统函数)用随机数驱动验证格式化输出parameter参数化定义,比如设计一个四位的DFF和2位的DFF,位......
  • 接口文档、jwt介绍和原理、drf-jwt快速使用、定制返回格式、jwt的认证类
    接口文档、jwt介绍和原理、drf-jwt快速使用、定制返回格式、jwt的认证类drf-接口文档我们接口编写好了之后,就需要编写接口文档,给前端的人使用前端开发人员根据后端人员......
  • POJ--3669 Meteor Shower(bfs/稍微变通)
    记录21:372023-2-9http://poj.org/problem?id=reference:《挑战程序设计竞赛(第2版)》第二章练习题索引p135DescriptionBessiehearsthatanextraordinarymeteor......
  • kubernetes(k8s)基础学习-kubernetes是什么?有什么用?
    kubernetes(k8s)基础学习-kubernetes是什么?一、认识DockerDocker是什么先来看看Docker的图标:一条鲸鱼背上驮着四方形块的物品,就像一条海运船上装满集装箱,集装箱里......