博客
关于我
设计一个验证系统
阅读量:361 次
发布时间:2019-03-05

本文共 1833 字,大约阅读时间需要 6 分钟。

原题指路

题目描述

你需要设计一个包含验证码的验证系统。每一次验证中,用户会收到一个新的验证码,这个验证码在 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.png

这道题其实最重要的一点就是为验证码的保存建立一个键值对的表,以便于验证码的各种更新与维护操作。在使用C++编程的时候,可以调用STL中的map模板来进行模拟。

代码

class AuthenticationManager {       private:        int ttl;        map
token; public: AuthenticationManager(int timeToLive) { ttl = timeToLive; } void generate(string tokenId, int currentTime) { token[tokenId] = currentTime + ttl; } void renew(string tokenId, int currentTime) { if(token.count(tokenId)) if(token[tokenId] > currentTime) token[tokenId] = currentTime + ttl; } int countUnexpiredTokens(int currentTime) { int cnt = 0; map
::iterator it; for(it = token.begin(); it != token.end(); it++) if(it->second > currentTime) cnt++; return cnt; }};/** * 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); */

转载地址:http://rpdg.baihongyu.com/

你可能感兴趣的文章
自定义Hive Sql Job分析工具
查看>>
【MySQL】(九)触发器
查看>>
关于Altium Designer 09导出BOM表不能正确分类问题
查看>>
Oracle 11G环境配置
查看>>
【Python】(十二)IO 文件处理
查看>>
【Oozie】(三)Oozie 使用实战教学,带你快速上手!
查看>>
师兄面试遇到这条 SQL 数据分析题,差点含泪而归!
查看>>
C语言的数值溢出问题(上)
查看>>
BottomNavigationView控件item多于3个时文字不显示
查看>>
函数指针的典型应用-计算函数的定积分(矩形法思想)
查看>>
8051单片机(STC89C52)八个LED灯闪烁
查看>>
8051单片机(STC89C52)以定时器中断模式实现两倒计时器异步计时
查看>>
用 wxPython 打印你的 App
查看>>
vue项目通过vue.config.js配置文件进行proxy反向代理跨域
查看>>
Linux下安装MySql过程
查看>>
android:使用audiotrack 类播放wav文件
查看>>
vue通过better-scroll 封装自定义的下拉刷新组件
查看>>
android解决:使用多线程和Handler同步更新UI
查看>>
vue自定义封装Loading组件
查看>>
Element UI 中动态路由的分析及实现
查看>>