LeetCode 1797. 设计一个验证系统(map)
生活随笔
收集整理的這篇文章主要介紹了
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
我的CSDN博客地址 https://michael.blog.csdn.net/
長(zhǎng)按或掃碼關(guān)注我的公眾號(hào)(Michael阿明),一起加油、一起學(xué)習(xí)進(jìn)步!
總結(jié)
以上是生活随笔為你收集整理的LeetCode 1797. 设计一个验证系统(map)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 2144. 打折购买糖
- 下一篇: LeetCode 642. 设计搜索自动