5693. 字符串中第二大的数字
给你一个混合字符串 s
,请你返回s
中 第二大 的数字,如果不存在第二大的数字,请你返回 -1 。
混合字符串 由小写英文字母和数字组成。
示例 1:
输入:s = "dfa12321afd"
输出:2
解释:出现在 s 中的数字包括 [1, 2, 3] 。第二大的数字是 2 。
示例 2:
输入:s = "abc1111"
输出:-1
解释:出现在 s 中的数字只包含 [1] 。没有第二大的数字。
class Solution {
public int secondHighest(String s) {
if(s.length() == 1) return -1;
Set<Integer> set = new TreeSet<>();
for(int i=0; i<s.length(); i++){
if(Character.isDigit(s.charAt(i))){
set.add(Integer.parseInt(String.valueOf(s.charAt(i))));
}
}
if(set.size() <= 1){
return -1;
}
int[] array = set.stream().mapToInt(Integer::intValue).toArray();
Arrays.sort(array);
return array[array.length - 2];
}
}
// class Solution {
// public int secondHighest(String s) {
// if(s.length() == 1) return -1;
// Set<Integer> set = new TreeSet<>();
// for(int i=0; i<s.length(); i++){
// if(Character.isDigit(s.charAt(i))){
// set.add(Integer.parseInt(String.valueOf(s.charAt(i))));
// }
// }
// return set.size() > 1 ? (int)set.toArray()[set.size() - 2] : -1;
// }
// }
首先创建一个哈希表,哈希表中的:
key : tokenId, value : 生成时间,也就是调用 generate 或者 renew 时带进来的 currentTime
5694. 设计一个验证系统
你需要设计一个包含验证码的验证系统。每一次验证中,用户会收到一个新的验证码,这个验证码在currentTime
时刻之后 timeToLive
秒过期。如果验证码被更新了,那么它会在 currentTime
(可能与之前的 currentTime
不同)时刻延长 timeToLive
秒。
请你实现AuthenticationManager
类:
AuthenticationManager(int timeToLive)
构造 AuthenticationManager
并设置 timeToLive
参数。
generate(string tokenId, int currentTime)
给定tokenId
,在当前时间 currentTime 生成一个新的验证码。
renew(string tokenId, int currentTime)
将给定tokenId
且 未过期 的验证码在 currentTime 时刻更新。如果给定 tokenId
对应的验证码不存在或已过期,请你忽略该操作,不会有任何更新操作发生。
countUnexpiredTokens(int currentTime)
请返回在给定 currentTime
时刻,未过期 的验证码数目。
如果一个验证码在时刻 t
过期,且另一个操作恰好在时刻t
发生(renew
或者 countUnexpiredTokens
操作),过期事件 优先于 其他操作。
示例 1:
输入:
["AuthenticationManager", "renew", "generate", "countUnexpiredTokens", "generate", "renew", "renew", "countUnexpiredTokens"] [[5], ["aaa", 1], ["aaa", 2], [6], ["bbb", 7], ["aaa", 8], ["bbb", 10], [15]]
输出:
[null, null, null, 1, null, null, null, 0]
解释:
AuthenticationManager authenticationManager = new AuthenticationManager(5); // 构造 AuthenticationManager ,设置 timeToLive = 5 秒。
authenticationManager.renew("aaa", 1); // 时刻 1 时,没有验证码的 tokenId 为 "aaa" ,没有验证码被更新。
authenticationManager.generate("aaa", 2); // 时刻 2 时,生成一个 tokenId 为 "aaa" 的新验证码。
authenticationManager.countUnexpiredTokens(6); // 时刻 6 时,只有 tokenId 为 "aaa" 的验证码未过期,所以返回 1 。
authenticationManager.generate("bbb", 7); // 时刻 7 时,生成一个 tokenId 为 "bbb" 的新验证码。
authenticationManager.renew("aaa", 8); // tokenId 为 "aaa" 的验证码在时刻 7 过期,且 8 >= 7 ,所以时刻 8 的renew 操作被忽略,没有验证码被更新。
authenticationManager.renew("bbb", 10); // tokenId 为 "bbb" 的验证码在时刻 10 没有过期,所以 renew 操作会执行,该 token 将在时刻 15 过期。
authenticationManager.countUnexpiredTokens(15); // tokenId 为 "bbb" 的验证码在时刻 15 过期,tokenId 为 "aaa" 的验证码在时刻 7 过期,所有验证码均已过期,所以返回 0 。
class AuthenticationManager {
private int timeToLive;
private Map<String, Integer> tokens;
public AuthenticationManager(int timeToLive) {
tokens = new HashMap<>();
this.timeToLive = timeToLive;
}
public void generate(String tokenId, int currentTime) {
tokens.put(tokenId, currentTime);
}
public void renew(String tokenId, int currentTime) {
if (tokens.containsKey(tokenId)) {
// 如果有的话,先记录下来它的上次生成时间
int time = tokens.get(tokenId);
// currentTime - time 就是上次生成时间和当前时间的差值
if (currentTime - time < timeToLive) tokens.put(tokenId, currentTime);
}
}
public int countUnexpiredTokens(int currentTime) {
int ans = 0;
for (Map.Entry<String, Integer> entry : tokens.entrySet()) {
if (currentTime - entry.getValue() < timeToLive) ++ans;
}
return ans;
}
}
5712. 你能构造出连续值的最大数目
给你一个长度为 n 的整数数组coins
,它代表你拥有的 n 个硬币。第 i 个硬币的值为 coins[i]
。如果你从这些硬币中选出一部分硬币,它们的和为 x
,那么称,你可以 构造 出x
。
请返回从 0 开始(包括 0 ),你最多能 构造 出多少个连续整数。
你可能有多个相同值的硬币。
示例 1:
输入:coins = [1,3]
输出:2
解释:你可以得到以下这些值:
- 0:什么都不取 []
- 1:取 [1]
从 0 开始,你可以构造出 2 个连续整数。
示例 2:
输入:coins = [1,1,1,4]
输出:8
解释:你可以得到以下这些值:
- 0:什么都不取 []
- 1:取 [1]
- 2:取 [1,1]
- 3:取 [1,1,1]
- 4:取 [4]
- 5:取 [4,1]
- 6:取 [4,1,1]
- 7:取 [4,1,1,1]
从 0 开始,你可以构造出 8 个连续整数。
示例 3:
输入:nums = [1,4,10,3,1]
输出:20
class Solution {
public int getMaximumConsecutive(int[] coins) {
Arrays.sort(coins);
int total = 0;
for (int x : coins) {
if (x > total + 1) {
break;
}
total += x;
}
return total + 1;
}
}