-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path681. Next Closest Time.js
72 lines (61 loc) · 1.47 KB
/
681. Next Closest Time.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
var nextClosestTime = function(time) {
time = time
.split(':')
.join('')
.split('');
var digits = [...time];
var max = ['2', '3', '5', '9'];
digits.sort((a, b) => a.localeCompare(b));
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
digits = digits.filter(onlyUnique);
var index = 3;
let flag = true;
while (flag) {
let temp = digits.indexOf(time[index]);
console.log(time[index].charCodeAt() < max[index].charCodeAt());
if (
time[index].charCodeAt() < max[index].charCodeAt() &&
temp < digits.length - 1
) {
if (
index === 3 &&
time[2] === '5' &&
digits[temp + 1].charCodeAt() <= max[index].charCodeAt()
) {
time[index] = digits[temp + 1];
flag = false;
} else {
index--;
}
if (index === 2 || index === 0) {
time[index] = digits[temp + 1];
flag = false;
}
if (
index === 1 &&
time[0] === '2' &&
digits[temp + 1].charCodeAt() <= max[index].charCodeAt()
) {
time[index] = digits[temp + 1];
flag = false;
} else {
index--;
}
} else {
if (index === 0) {
time[0] = time[1] = time[2] = time[3] = digits[0];
flag = false;
} else if (0 < index) {
index--;
}
}
}
return `${time[0]}${time[1]}:${time[2]}${time[3]}`;
};
// console.log(nextClosestTime('19:34'));
// console.log(nextClosestTime('23:59'));
// console.log(nextClosestTime('23:53'));
// console.log(nextClosestTime('12:01'));
console.log(nextClosestTime('13:55'));