문제
Split a duration into hours, minutes, and seconds. Write units as h, m, and s, in that order, with one space between units. Omit any zero-valued unit, except that zero seconds is written as 0s.
Implement format_duration(seconds).
Input
seconds: The total duration in seconds.
Output
Return the formatted duration, for example 2h 2m 5s.
제약 조건
0 ≤ seconds ≤ 1,000,000,000. Hours may exceed 23; this is a duration, not a time of day.
예시
입력:
seconds = 7325
예상 출력:
expected = "2h 2m 5s"
입력:
long long seconds = 7325;
예상 출력:
std::string expected = "2h 2m 5s";
입력:
long seconds = 7325;
예상 출력:
string expected = "2h 2m 5s";
설명
7325 = 2 × 3600 + 2 × 60 + 5.
입력:
seconds = 120
예상 출력:
expected = "2m"
입력:
long long seconds = 120;
예상 출력:
std::string expected = "2m";
입력:
long seconds = 120;
예상 출력:
string expected = "2m";
설명
120 seconds is exactly 2 minutes. Omit the zero hours and seconds.
접근 방법
먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.
힌트 1
Break the total seconds into larger units first, then format only the nonzero parts.
힌트 2
Keep hours, minutes, and seconds in that order, with spaces between included units.
힌트 3
If every larger unit is zero, still make sure the output is exactly 0s when the duration is zero.
예상 복잡도
O(1) time, O(1) extra space
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.