日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > windows >内容正文

windows

LeetCode 1797. 设计一个验证系统(map)

發(fā)布時(shí)間:2024/7/5 windows 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode 1797. 设计一个验证系统(map) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • 1. 題目
    • 2. 解題

1. 題目

你需要設(shè)計(jì)一個(gè)包含驗(yàn)證碼的驗(yàn)證系統(tǒng)。
每一次驗(yàn)證中,用戶會(huì)收到一個(gè)新的驗(yàn)證碼,這個(gè)驗(yàn)證碼在 currentTime 時(shí)刻之后 timeToLive 秒過期。
如果驗(yàn)證碼被更新了,那么它會(huì)在 currentTime (可能與之前的 currentTime 不同)時(shí)刻延長(zhǎng) timeToLive 秒。

請(qǐng)你實(shí)現(xiàn) AuthenticationManager 類:

  • AuthenticationManager(int timeToLive) 構(gòu)造 AuthenticationManager 并設(shè)置 timeToLive 參數(shù)。
  • generate(string tokenId, int currentTime) 給定 tokenId ,在當(dāng)前時(shí)間 currentTime 生成一個(gè)新的驗(yàn)證碼。
  • renew(string tokenId, int currentTime) 將給定 tokenId 且 未過期 的驗(yàn)證碼在 currentTime 時(shí)刻更新。如果給定 tokenId 對(duì)應(yīng)的驗(yàn)證碼不存在或已過期,請(qǐng)你忽略該操作,不會(huì)有任何更新操作發(fā)生。
  • countUnexpiredTokens(int currentTime) 請(qǐng)返回在給定 currentTime 時(shí)刻,未過期 的驗(yàn)證碼數(shù)目。

如果一個(gè)驗(yàn)證碼在時(shí)刻 t 過期,且另一個(gè)操作恰好在時(shí)刻 t 發(fā)生(renew 或者 countUnexpiredTokens 操作),過期事件 優(yōu)先于 其他操作

示例 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); // 構(gòu)造 AuthenticationManager ,設(shè)置 timeToLive = 5 秒。 authenticationManager.renew("aaa", 1); // 時(shí)刻 1 時(shí),沒有驗(yàn)證碼的 tokenId 為 "aaa" ,沒有驗(yàn)證碼被更新。 authenticationManager.generate("aaa", 2); // 時(shí)刻 2 時(shí),生成一個(gè) tokenId 為 "aaa" 的新驗(yàn)證碼。 authenticationManager.countUnexpiredTokens(6); // 時(shí)刻 6 時(shí),只有 tokenId 為 "aaa" 的驗(yàn)證碼未過期,所以返回 1 。 authenticationManager.generate("bbb", 7); // 時(shí)刻 7 時(shí),生成一個(gè) tokenId 為 "bbb" 的新驗(yàn)證碼。 authenticationManager.renew("aaa", 8); // tokenId 為 "aaa" 的驗(yàn)證碼在時(shí)刻 7 過期,且 8 >= 7 ,所以時(shí)刻 8 的renew 操作被忽略,沒有驗(yàn)證碼被更新。 authenticationManager.renew("bbb", 10); // tokenId 為 "bbb" 的驗(yàn)證碼在時(shí)刻 10 沒有過期,所以 renew 操作會(huì)執(zhí)行,該 token 將在時(shí)刻 15 過期。 authenticationManager.countUnexpiredTokens(15); // tokenId 為 "bbb" 的驗(yàn)證碼在時(shí)刻 15 過期,tokenId 為 "aaa" 的驗(yàn)證碼在時(shí)刻 7 過期,所有驗(yàn)證碼均已過期,所以返回 0 。提示: 1 <= timeToLive <= 108 1 <= currentTime <= 108 1 <= tokenId.length <= 5 tokenId 只包含小寫英文字母。 所有 generate 函數(shù)的調(diào)用都會(huì)包含獨(dú)一無二的 tokenId 值。 所有函數(shù)調(diào)用中,currentTime 的值 嚴(yán)格遞增 。 所有函數(shù)的調(diào)用次數(shù)總共不超過 2000 次。

https://leetcode-cn.com/problems/design-authentication-manager/

2. 解題

  • 使用 雙向 map 記錄 token 和 time,題目說了都是獨(dú)一無二的 key
class AuthenticationManager {int livetime;map<int,string> t_str;unordered_map<string, int> str_t; public:AuthenticationManager(int timeToLive) {livetime = timeToLive;}void generate(string tokenId, int currentTime) {t_str[currentTime] = tokenId;str_t[tokenId] = currentTime;}void renew(string tokenId, int currentTime) {if(!str_t.count(tokenId)) return;int t = str_t[tokenId];if(currentTime-t < livetime) {t_str.erase(t);t_str[currentTime] = tokenId;str_t[tokenId] = currentTime;}}int countUnexpiredTokens(int currentTime) {for(auto it = t_str.begin(); it != t_str.end(); ) {int t = it->first;string token = it->second;if(currentTime-t >= livetime){ // 刪除過期的t_str.erase(it++);str_t.erase(token);}else // map key 有序,沒過期,停止刪除break;}return str_t.size();} };

我的CSDN博客地址 https://michael.blog.csdn.net/

長(zhǎng)按或掃碼關(guān)注我的公眾號(hào)(Michael阿明),一起加油、一起學(xué)習(xí)進(jìn)步!

總結(jié)

以上是生活随笔為你收集整理的LeetCode 1797. 设计一个验证系统(map)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。