[Java] 5694. Design a verification system---use key-value to solve the relationship between two variables! ! !

You need to design a verification system that includes verification codes. During each verification, the user will receive a new verification code, which expires timeToLive seconds after the currentTime. If the verification code is updated, it will extend timeToLive seconds at the currentTime (may be different from the previous currentTime).

Please implement the AuthenticationManager class:

AuthenticationManager(int timeToLive) Constructs the AuthenticationManager and sets the timeToLive parameter.
generate(string tokenId, int currentTime) Given the tokenId, generate a new verification code at the current time currentTime.
renew(string tokenId, int currentTime) Update the verification code with the given tokenId and not expired at currentTime. If the verification code corresponding to the given tokenId does not exist or has expired, please ignore this operation and no update operation will occur.
countUnexpiredTokens(int currentTime) Please return the number of verification codes that have not expired at the given currentTime.
If a verification code expires at time t, and another operation happens to happen at time t (renew or countUnexpiredTokens operation), the expiration event takes precedence over other operations.

Example 1:
Insert picture description here

Input:
["AuthenticationManager", "renew", "generate", "countUnexpiredTokens", "generate", "renew", "renew", "countUnexpiredTokens"]
[[5], ["aaa", 1], [" aaa", 2], [6], ["bbb", 7], ["aaa", 8], ["bbb", 10], [15]]
Output:
[null, null, null, 1, null , null, null, 0]

Explanation:
AuthenticationManager authenticationManager = new AuthenticationManager(5); // Construct AuthenticationManager and set timeToLive = 5 seconds.
authenticationManager.renew("aaa", 1); // At time 1, the tokenId without verification code is "aaa" and no verification code is updated.
authenticationManager.generate("aaa", 2); // At time 2, a new verification code with tokenId "aaa" is generated.
authenticationManager.countUnexpiredTokens(6); // At time 6, only the verification code whose tokenId is "aaa" has not expired, so 1 is returned.
authenticationManager.generate("bbb", 7); // At time 7, a new verification code with tokenId "bbb" is generated.
authenticationManager.renew("aaa", 8); // The verification code whose tokenId is "aaa" expires at time 7, and 8 >= 7, so the renew operation at time 8 is ignored and no verification code is updated.
authenticationManager.renew("bbb", 10); // The verification code whose tokenId is "bbb" does not expire at time 10, so the renew operation will be executed, and the token will expire at time 15.
authenticationManager.countUnexpiredTokens(15); // The verification code with tokenId "bbb" expires at time 15, and the verification code with tokenId "aaa" expires at time 7. All verification codes have expired, so 0 is returned.

prompt:

1 <= timeToLive <= 108
1 <= currentTime <= 108
1 <= tokenId.length <= 5 tokenId
contains only lowercase English letters.
All calls to the generate function will contain a unique tokenId value.
In all function calls, the value of currentTime is strictly incremented.
The total number of calls of all functions does not exceed 2000 times.

代码:
private int timeToLive;
	private HashMap<String,Integer> hashMap=new HashMap<>();
	public AuthenticationManager(int timeToLive) {
    
    
        this.timeToLive=timeToLive;
    }
    
    public void generate(String tokenId, int currentTime) {
    
    
        this.hashMap.put(tokenId,currentTime+this.timeToLive);
    }
    
    public void renew(String tokenId, int currentTime) {
    
    
    	
        if(this.hashMap.containsKey(tokenId)&&this.hashMap.get(tokenId)>currentTime) {
    
    
        	 this.hashMap.put(tokenId,currentTime+this.timeToLive);
        }else {
    
    
        	this.hashMap.remove(tokenId);
        }
    }
    
    public int countUnexpiredTokens(int currentTime) {
    
    
    	int i=0;
		for (Map.Entry<String,Integer> a: this.hashMap.entrySet()) {
    
    
			 if(a.getValue()>currentTime) {
    
    
				 i++;
			 }
		}
    	return i;
    }

Guess you like

Origin blog.csdn.net/qq_44461217/article/details/115040540