题目:
leetcode.cn/problems/de…
算法:
方法一:hash table
这种题目做100遍也不能提高了,搞点有难度的
type AuthenticationManager struct {
TimeToLive int
TokenExpireTime map[string]int
}
func Constructor(timeToLive int) AuthenticationManager {
return AuthenticationManager{
TimeToLive: timeToLive,
TokenExpireTime: make(map[string]int),
}
}
func (this *AuthenticationManager) Generate(tokenId string, currentTime int) {
this.TokenExpireTime[tokenId] = this.TimeToLive + currentTime
}
func (this *AuthenticationManager) Renew(tokenId string, currentTime int) {
if t, ok := this.TokenExpireTime[tokenId]; ok {
if currentTime < t {
this.TokenExpireTime[tokenId] = currentTime + this.TimeToLive
}
}
}
// 二分查找
func (this *AuthenticationManager) CountUnexpiredTokens(currentTime int) int {
count := 0
for _, t := range this.TokenExpireTime {
if currentTime < t {
count ++
}
}
return count
}