3986. Number of Elapsed Seconds Between Two Times
You are given two valid times startTime and endTime, each represented as a string in the format "HH:MM:SS".
Return the number of seconds that have elapsed from startTime to endTime.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Solution { array<int,3> parse(string& s) { auto digitOf = [&](char ch) { return ch - '0'; }; return {digitOf(s[0]) * 10 + digitOf(s[1]), digitOf(s[3]) * 10 + digitOf(s[4]), digitOf(s[6]) * 10 + digitOf(s[7])}; } long long time(string& str) { auto [h,m,s] = parse(str); return h * 3600 + m * 60 + s; } public: int secondsBetweenTimes(string startTime, string endTime) { return time(endTime) - time(startTime); } };
|