网站建设品牌,wordpress后台 插件,怎么修改网站的源代码,seo网站优化知识题目链接 Leetcode.1797 设计一个验证系统 Rating : 1534 题目描述
你需要设计一个包含验证码的验证系统。每一次验证中#xff0c;用户会收到一个新的验证码#xff0c;这个验证码在 currentTime时刻之后 timeToLive秒过期。如果验证码被更新了#xff0c;那么它会在 curr…题目链接 Leetcode.1797 设计一个验证系统 Rating : 1534 题目描述
你需要设计一个包含验证码的验证系统。每一次验证中用户会收到一个新的验证码这个验证码在 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 。 提示
1timeToLive1081 timeToLive 10^81timeToLive1081currentTime1081 currentTime 10^81currentTime1081tokenId.length51 tokenId.length 51tokenId.length5tokenId只包含小写英文字母。所有 generate函数的调用都会包含独一无二的 tokenId值。所有函数调用中currentTime的值 严格递增 。所有函数的调用次数总共不超过 2000次。
分析
用一个 哈希表 key代表验证码value代表过期时间进行模拟即可。
时间复杂度O(1)O(1)O(1)
代码
class AuthenticationManager {
public:int timeToLive;unordered_mapstring,int m;AuthenticationManager(int timeToLive) {this-timeToLive timeToLive;}void generate(string tokenId, int currentTime) {m[tokenId] currentTime timeToLive;}void renew(string tokenId, int currentTime) {//先去掉已经过期的验证码for(auto it m.begin();it ! m.end();){if(it-second currentTime) m.erase(it);else it;}//再更新if(m.count(tokenId)) m[tokenId] currentTime timeToLive;}int countUnexpiredTokens(int currentTime) {int ans 0;for(auto [k,v]:m){//记录未过期的验证码数量if(v currentTime) ans;}return ans;}
};/*** 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);*/