Skip to content

Commit fe22304

Browse files
committed
1797. 设计一个验证系统
1 parent f5697a8 commit fe22304

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
|1664|[生成平衡数组的方案数](https://leetcode.cn/problems/ways-to-make-a-fair-array/)|[JavaScript](./algorithms/ways-to-make-a-fair-array.js)|Medium|
203203
|1669|[合并两个链表](https://leetcode.cn/problems/merge-in-between-linked-lists/)|[JavaScript](./algorithms/merge-in-between-linked-lists.js)|Medium|
204204
|1780|[判断一个数字是否可以表示成三的幂的和](https://leetcode.cn/problems/check-if-number-is-a-sum-of-powers-of-three/)|[JavaScript](./algorithms/check-if-number-is-a-sum-of-powers-of-three.js)|Medium|
205+
|1797|[设计一个验证系统](https://leetcode.cn/problems/design-authentication-manager/)|[JavaScript](./algorithms/design-authentication-manager.js)|Medium|
205206
|1807|[替换字符串中的括号内容](https://leetcode.cn/problems/evaluate-the-bracket-pairs-of-a-string/)|[JavaScript](./algorithms/evaluate-the-bracket-pairs-of-a-string.js)|Medium|
206207
|1813|[句子相似性 III](https://leetcode.cn/problems/sentence-similarity-iii/)|[JavaScript](./algorithms/sentence-similarity-iii.js)|Medium|
207208
|1814|[统计一个数组中好对子的数目](https://leetcode.cn/problems/count-nice-pairs-in-an-array/)|[JavaScript](./algorithms/count-nice-pairs-in-an-array.js)|Medium|
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @param {number} timeToLive
3+
*/
4+
var AuthenticationManager = function(timeToLive) {
5+
this.timeToLive = timeToLive;
6+
this.map = new Map(); // tokenId -> liveTime (latest)
7+
};
8+
9+
/**
10+
* @param {string} tokenId
11+
* @param {number} currentTime
12+
* @return {void}
13+
*/
14+
AuthenticationManager.prototype.generate = function(tokenId, currentTime) {
15+
this.map.set(tokenId, currentTime + this.timeToLive);
16+
};
17+
18+
/**
19+
* @param {string} tokenId
20+
* @param {number} currentTime
21+
* @return {void}
22+
*/
23+
AuthenticationManager.prototype.renew = function(tokenId, currentTime) {
24+
const liveTime = this.map.get(tokenId);
25+
if (this.map.has(tokenId) && liveTime > currentTime) {
26+
this.map.set(tokenId, currentTime + this.timeToLive);
27+
}
28+
};
29+
30+
/**
31+
* @param {number} currentTime
32+
* @return {number}
33+
*/
34+
AuthenticationManager.prototype.countUnexpiredTokens = function(currentTime) {
35+
let count = 0;
36+
this.map.forEach((item) => {
37+
if (item > currentTime) {
38+
count++;
39+
} else {
40+
this.map.delete(key); // 移除过期的 token
41+
}
42+
});
43+
return count;
44+
};
45+
46+
47+
/**
48+
* Your AuthenticationManager object will be instantiated and called as such:
49+
* var obj = new AuthenticationManager(timeToLive)
50+
* obj.generate(tokenId,currentTime)
51+
* obj.renew(tokenId,currentTime)
52+
* var param_3 = obj.countUnexpiredTokens(currentTime)
53+
*/

0 commit comments

Comments
 (0)