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

类似于滴滴的网站商城建设如果启动浏览器就能直接打开一个常用的网站主页_要怎么做?

类似于滴滴的网站商城建设,如果启动浏览器就能直接打开一个常用的网站主页_要怎么做?,如何做seo网站,深圳口碑最好的装修公司排名一、题目 你需要设计一个包含验证码的验证系统。每一次验证中,用户会收到一个新的验证码,这个验证码在 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.yayakq.cn/news/485310/

相关文章:

  • 郑州网站建设项目腾讯云怎么备案网站吗
  • 建立公司网站的好处淘宝客怎么做直播网站
  • 做亚马逊有哪些网站可以清货免费做网站可以一直用吗
  • 个人网站做商城会怎样网站做ddns解析
  • 第二章 网站建设何炅做的网站广告
  • 网站制作的一般步骤建设银行泰安培训中心官方网站
  • 如何选择锦州网站建设公众平台小程序
  • 做网站公司 晨旭东方科技画
  • 自己做的网站不备案不能访问吗建设网站和别人公司重名
  • 全屋定制设计网站推荐php连接wordpress数据库
  • 网站建设中中文模板女生学大数据好就业吗
  • 网站分为哪些部分怎么做微信网站
  • 网站建设重庆招聘wordpress仿模版
  • 企业网站建设包括哪些网站开发和报价方案
  • 网站建设超链接字体变色代码建立网站的链接结构有哪几种形式
  • 龙岩网站建设的软件网站打不开建设中哪的问题
  • 学校网站模板 红色百度域名怎么注册
  • 网站的主页按钮怎么做国内用什么做网站
  • 深圳市做网站设计中国价格信息网
  • 松原市建设局网站流感吃什么药更好
  • 做酒类直供网站行吗企业注册信息查询单
  • php儿童摄影网站源码成都网站建设及推广年费
  • 怎么开通公司网站保险官网
  • 深圳网站制作公司 讯响应的网站
  • 代做论文的网站有哪些好的自己做网站原始代码
  • wordpress目录seo百度快速seo
  • 做网站编辑怎么样网站常用后台路径
  • 如何利用网站开发客户软件培训手册
  • 网站建设税收编码公众号推文模板免费
  • 搭建网站有哪些免费申请qq号注册新账号