forked from dipsdeepak11/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
681_Next_Closest_Time.java
59 lines (51 loc) · 1.45 KB
/
681_Next_Closest_Time.java
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
56
57
58
59
// time: 4 * 10 | space: 10
class Solution {
public String nextClosestTime(String time) {
if (time == null || time.length() == 0) {
return time;
}
char[] t = time.toCharArray();
char[] res = new char[4];
int[] list = new int[10];
char min = '9';
for (char c : t) {
if (c == ':') {
continue;
}
list[c - '0'] = 1;
if (c < min) {
min = c;
}
}
for (int i = t[4] - '0' + 1; i <= 9; i++) {
if (list[i] > 0) {
t[4] = (char)(i + '0');
return new String(t);
}
}
t[4] = min;
for (int i = t[3] - '0' + 1; i <= 5; i++) {
if (list[i] > 0) {
t[3] = (char)(i + '0');
return new String(t);
}
}
t[3] = min;
int stop = t[0] == '2' ? 3 : 9;
for (int i = t[1] - '0' + 1; i <= stop; i++) {
if (list[i] > 0) {
t[1] = (char)(i + '0');
return new String(t);
}
}
t[1] = min;
for (int i = t[0] - '0' + 1; i <= 2; i++) {
if (list[i] > 0) {
t[0] = (char)(i + '0');
return new String(t);
}
}
t[0] = min;
return new String(t);
}
}