2 Star 0 Fork 0

xinanXu/myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LC1797.cpp 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
class AuthenticationManager {
public:
void MapSortOfValue(vector<pair<int,string> >& vec,map<string,int>& m) {
for (map<string, int>::iterator it = m.begin(); it != m.end(); it++)
vec.push_back(make_pair(it->second, it->first));
sort(vec.begin(), vec.end());
}
int t2l;
map<string, int> msg;
AuthenticationManager(int timeToLive) {
t2l = timeToLive;
}
void generate(string tokenId, int currentTime) {
auto it = msg.find(tokenId);
if (it == msg.end()) {
msg.insert({tokenId, currentTime});
it = msg.find(tokenId);
}
else it->second = currentTime;
}
void renew(string tokenId, int currentTime) {
auto it = msg.find(tokenId);
if (it != msg.end()) {
if (it->second + t2l > currentTime)
it->second = currentTime;
else
msg.erase(it);
}
}
int countUnexpiredTokens(int currentTime) {
int ret = 0;
vector<pair<int,string>>v;
MapSortOfValue(v, msg);
cout << currentTime << endl;
int len = v.size();
for (int i = len - 1; i >= 0; i--) {
if (v[i].first + t2l > currentTime)
ret += 1;
else break;
}
return ret;
}
};
/**
* Your AuthenticationManager object will be instantiated and called as such:
* AuthenticationManager* obj = new AuthenticationManager(timeToLive);
* obj->generate(tokenId,currentTime);
* obj->renew(tokenId,currentTime);
* int param_3 = obj->countUnexpiredTokens(currentTime);
*/
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/DearAtri/myleetcode.git
git@gitee.com:DearAtri/myleetcode.git
DearAtri
myleetcode
myleetcode
master

搜索帮助