当前位置: 首页 > news >正文

厦门网站建设公司写软文平台

厦门网站建设公司,写软文平台,腾达企业交换机管理网站,网站 做百度推广有没有效果一、题目 你需要设计一个包含验证码的验证系统。每一次验证中,用户会收到一个新的验证码,这个验证码在 currentTime 时刻之后 timeToLive 秒过期。如果验证码被更新了,那么它会在 currentTime (可能与之前的 currentTime 不同&am…

一、题目

你需要设计一个包含验证码的验证系统。每一次验证中,用户会收到一个新的验证码,这个验证码在 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 操作),过期事件 优先于 其他操作。

示例

在这里插入图片描述
在这里插入图片描述

来源:力扣(LeetCode)
链接:

二、C++解法

我的思路及代码

此代码在力扣超时,但是我觉得力扣评判的不标准
用两个哈希表,第一个存储token到期时间,第二个存储当前时间有几个未过期的token。
新建操作:更新token的到期时间,并且更新这个持续时间中的token数量。
更新操作:判断当前token是否到期,若还有效则更新token的到期时间,并且从原来的到期时间开始直到新的到期时间结束的时间内继续增加token数量。

class AuthenticationManager {
public:int timeToLive;unordered_map<string,int> tokenAndTimePast;unordered_map<int,int> countTokens;AuthenticationManager(int timeToLive) {this->timeToLive = timeToLive;}void generate(string tokenId, int currentTime) {tokenAndTimePast[tokenId] = currentTime+timeToLive;for(int i=currentTime;i<currentTime+timeToLive;i++){countTokens[i]++;}}void renew(string tokenId, int currentTime) {//当tokenId不存在的时候,他对应的值为0,所以可以用这个条件来判断if(tokenAndTimePast[tokenId]>currentTime){for(int i=tokenAndTimePast[tokenId];i<currentTime+timeToLive;i++){countTokens[i]++;}tokenAndTimePast[tokenId] = currentTime+timeToLive;}}int countUnexpiredTokens(int currentTime) {return countTokens[currentTime];}
};/*** 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);*/
  • 时间复杂度:
  • 构造函数:O(1)
  • generate:O(n),其中 n 为 currentTime 的秒数
  • renew:O(n),其中 n 为 currentTime 的秒数
  • 官方写的:countUnexpiredTokens:O(n),其中 n 为 generate 的调用次数。
  • 空间复杂度:O(n),两个 map 中 n 都为 generate 的调用次数。

官方参考代码

class AuthenticationManager {
private:int timeToLive;unordered_map<string, int> mp;
public: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;}
};
  • 时间复杂度:
  • 构造函数:O(1)
  • generate:O(1)
  • renew:O(1)
  • 官方写的:countUnexpiredTokens:O(n),其中 n 为 generate 的调用次数。我认为的:countUnexpiredTokens里面带有 for 循环,循环次数和 mp 的个数相关,而 mp 的个数和 tokenId 的数量相关,所以我认为是 countUnexpiredTokens :O(nt),其中 n 为 generate 的调用次数,t 为 tokenId 的数量
  • 空间复杂度:O(n),其中 n 为 generate 的调用次数,map 中有 n 个元素。
http://www.wangmingla.cn/news/132018.html

相关文章:

  • 做淘宝优惠券推广网站查网站流量查询工具
  • 做爰片的网站简单制作html静态网页
  • 杭州设计网站的公司企业文化
  • 小程序源代码seo的基本步骤顺序正确的是
  • 做a爱片网站软文推广文章范文1000
  • 弄美团网站的一般一个做赚多少钱深圳优化公司高粱seo较
  • 临沂最好的做网站公司google登录入口
  • 哈尔滨道里建设局网站北京知名seo公司精准互联
  • 重庆免费公司建网站系统优化大师官方下载
  • 魏县网站制作软件开发app制作公司
  • 做旅游的网站有哪些推广普通话手抄报内容怎么写
  • 福州网站建设方案推广注册自己的网站
  • 自己做的网站提示不安全网红推广接单平台
  • 做网站兼容性如何处理国外seo
  • 做app原型的网站哪里有营销策划培训班
  • 宁波定制网站建设温州企业网站排名优化
  • 维护网站都干什么软文营销网站
  • 怎么做英文版网站seo项目经理
  • 中国建设银行有哪些招聘网站制作网站需要多少费用
  • 韩雪冬做网站多少钱济南做网站建设的公司
  • wordpress底部菜单爱站seo工具包官网
  • 网站顶部有空白我想做网络推广
  • 南昌一建集团有限公司seo搜索引擎优化总结
  • 公司网站设计 上海山东济南seo整站优化费用
  • 做类型网站国外b站不收费免费2023
  • 学习之家网站出售友情链接是什么意思
  • 银行做网站视频环球网最新消息疫情
  • 网站如何做微信登录最厉害的搜索引擎
  • 巨野网站建设网络优化培训要多少钱
  • 手机网站开发升上去怎么样才可以在百度上打广告