Next Closest Time
Description
null
Solution(javascript)
/**
* @param {string} time
* @return {string}
*/
const nextClosestTime = function (time) {
const [a, b, c, d] = [time[0], time[1], time[3], time[4]].map(x => Number(x))
const sorted = [a, b, c, d].sort((x, y) => x - y)
const d2 = sorted.find(x => x > d)
if (d2 > d) { // 分钟末位
return `${a}${b}:${c}${d2}`
}
const c2 = sorted.find(x => x > c && x <= 5) // 时分位
const min = Math.min(a, b, c, d)
if (c2 > c) {
return `${a}${b}:${c2}${min}`
}
const b2 = sorted.find(x => x > b && a * 10 + x <= 24)
if (b2 > b) {
return `${a}${b2}:${min}${min}`
}
const a2 = sorted.find(x => x > a && x <= 2)
if (a2 > a) {
return `${a2}${min}:${min}${min}`
}
return `${min}${min}:${min}${min}`
}