這題就比較單純了, 就是用一個hashmap去做紀錄
class AuthenticationManager {
public:
AuthenticationManager(int timeToLive) {
t2l = timeToLive;
}
void generate(string tokenId, int currentTime) {
m[tokenId] = currentTime + t2l;
}
void renew(string tokenId, int currentTime) {
if(!m.count(tokenId)){
return;
}else{
if(m[tokenId] <= currentTime){
return;
}else{
m[tokenId] = currentTime + t2l;
}
}
}
int countUnexpiredTokens(int currentTime) {
int res = 0;
for(auto i:m){
if(i.second > currentTime){
res++;
}
}
return res;
}
private:
unordered_map<string, int> m;
int t2l;
};