Latest Time by Replacing Hidden Digits LeetCode solution in C++

1
class Solution {
2
public:
3
string maximumTime(string time) {
4
// for time[0]
5
// if both characters of hour is ?, then hour is 23 then time[0] should be
6
// '2'("??:3?)". if 2nd character of hour is <= 3, then hour can be in 20s
7
// then time[0] should be '2'. if 2nd character of hour is >3, then hour can
8
// only be in 10s then time[0] should be '1'.
9
if (time[0] == '?') time[0] = (time[1] <= '3' || time[1] == '?') ? '2' : '1';
10
// if 1st character of hour is 0 or 1, then hour can be 09 or 19 then
11
// time[1] should be '9'. if 1st character of hour is 2, then hour can be 23
12
// then time[1] should be '3'.
13
if (time[1] == '?') time[1] = time[0] == '2' ? '3' : '9';
14
// if both characters of minute is ? then minute is 59, or only 4th
15
// character is ? then 5_ so time[3] is always '5'.
16
if (time[3] == '?') time[3] = '5';
17
// if 2nd character of minute is ?, then time[4] is '9'.
18
if (time[4] == '?') time[4] = '9';
19
return time;
20
}
21
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0