1797. 设计一个验证系统

你需要设计一个包含验证码的验证系统。每一次验证中,用户会收到一个新的验证码,这个验证码在 currentTime 时刻之后 timeToLive 秒过期。如果验证码被更新了,那么它会在 currentTime (可能与之前的 currentTime 不同)时刻延长 timeToLive 秒。
请你实现 AuthenticationManager 类:
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/design-authentication-manager
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

莫名其妙的一道题,题干描述莫名其妙,通过的也莫名其妙,没啥营养,也不知道有啥优化方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class AuthenticationManager(object):

def __init__(self, timeToLive):
"""
:type timeToLive: int
"""
self.timeToLive = timeToLive
self.token_limit = dict()

def generate(self, tokenId, currentTime):
"""
:type tokenId: str
:type currentTime: int
:rtype: None
"""
self.token_limit[tokenId] = currentTime + self.timeToLive

def renew(self, tokenId, currentTime):
"""
:type tokenId: str
:type currentTime: int
:rtype: None
"""
if tokenId in self.token_limit:
if currentTime < self.token_limit[tokenId]:
self.token_limit[tokenId] = currentTime + self.timeToLive

def countUnexpiredTokens(self, currentTime):
"""
:type currentTime: int
:rtype: int
"""
count = 0
for k,v in self.token_limit.items():
if currentTime < v:
count += 1
return count
# Your AuthenticationManager object will be instantiated and called as such:
# obj = AuthenticationManager(timeToLive)
# obj.generate(tokenId,currentTime)
# obj.renew(tokenId,currentTime)
# param_3 = obj.countUnexpiredTokens(currentTime)