当前位置: 首页 > 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.dinnco.com/news/17811.html

相关文章:

  • 网站安全性设计培训心得体会1500字
  • 官方网站建设思路百度引流推广
  • 网站换ip 有多大影响个人怎么注册自己的网站
  • python做网站用什么软件网店运营具体做什么
  • 青岛市做网站生意参谋官网
  • 网站排名提升软件推客平台
  • java与php哪个做网站好百度查找相似图片
  • 做网站申请域名大概花费多少网络营销公司有哪些公司
  • 如何做淘宝客有没有免费的网站阿里云搜索引擎
  • 广州手机网站制作云盘网页版登录
  • 前端只是做网站吗亚马逊跨境电商个人开店
  • 上海企业网站排名优化销售系统
  • 政府部门政府网站建设谷歌推广公司
  • 十堰网站免费建设百度推广seo是什么意思
  • 广东网站系统建设电子商务营销策划方案
  • 网站注册备案之后怎么做seo超级外链工具
  • 做卷闸门网站有用吗百度推广咨询
  • 企业自己做网站方法营销方法有哪些
  • 商业网站设计专业seo个人博客
  • 建设微信商城网站制作网站seo外链
  • 如何看网站是否正常搜索引擎优化的核心及内容
  • 曲阜网站建设价格自建网站平台
  • 做网站用图片晋城网站seo
  • 做网站多少费用中国十大软件外包公司
  • 天猫商城官方网站营销方案包括哪些内容
  • 谷歌seo网站怎么做产品分类网页优化方法
  • 网站流量 收益营销策略ppt模板
  • 微投票网站网络营销的企业有哪些
  • 长沙市建网站百度动态排名软件
  • 哪个网站可以做c 的项目京东关键词优化技巧