챌린지 목록으로 돌아가기
쉬움 0명 해결 정답률 0%

Hours, Minutes, and Seconds

Convert a duration in seconds to a compact readable string.

배우게 될 내용

  • Split seconds into hours, minutes and seconds
  • Use division and remainder together
  • Format parts into a readable string

문제

번역본 보기

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.

예시

예시 1
입력:
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.

예시 2
입력:
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에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.