Google OA(HH:MM)

input给一个format “00:00” - “23:59” 的时间,里面每位数字可以重复使用,问组成的下一个时间是多少。 例23:59 —> 22:22

直接暴力

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
public String nextTime(String time) {
StringBuffer res = new StringBuffer();
TreeSet<Integer> set = new TreeSet<>();
for(int i=0; i<5; i++){
if (i == 2) continue;
set.add(time.charAt(i)-48);
}
int point = 0;
// 最后一位
if (set.higher(time.charAt(4)-48) == null) point = 1;
if (point == 1) res.append(set.first());
else res.append(set.higher(time.charAt(4)-48));
// 倒数第二
if (point == 0) res.append(time.charAt(3));
else {
if (set.higher(time.charAt(3) - 48) == null || set.higher(time.charAt(3) - 48) > 5)
res.append(set.first());
else {
res.append(set.higher(time.charAt(3) - 48));
point = 0;
}
}
res.append(':');
if (point == 0) res.append(time.charAt(1));
else {
if (time.charAt(0) == 2) {
if (set.higher(time.charAt(1) - 48) == null || set.higher(time.charAt(1) - 48) > 3)
res.append(set.first());
else {
res.append(set.higher(time.charAt(1) - 48));
point = 0;
}
}
else{
if (set.higher(time.charAt(1) - 48) == null)
res.append(set.first());
else {
res.append(set.higher(time.charAt(1) - 48));
point = 0;
}
}
}
if (point == 0) res.append(time.charAt(0));
else{
if (set.higher(time.charAt(0)-48) == null || set.higher(time.charAt(0)-48) > 2) res.append(set.first());
else res.append(set.higher(time.charAt(0)-48));
}
res.reverse();
return res.toString();
}
}

自己随便测试了一下,应该可行。