Notice
Recent Posts
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- mrc
- SWIFT
- IOS
- 클린코드
- 카카오맵클론
- 프로그래머스
- 앱의생명주기
- five lines of cdde
- firebase
- alamofire
- css학습
- Safari Inspector
- Swift디자인패턴
- ios면접
- hackerrank
- RC
- TDD
- storekit2
- 코딩테스트입문
- unittest
- firestore
- algorithm
- Swift코딩테스트
- RxSwift
- AutoLayout
- ARC
- 리팩터링
- five lines of code
- UIKit
- Di
Archives
- Today
- Total
샘성의 iOS 개발 일지
[Warm Up] Time Conversion 본문
728x90
문제 설명:
Given a time in -hour AM/PM format, convert it to military (24-hour) time.
Note: - 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
- 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.
Example
* s = '12:01:00 PM'
Return '12:01:00'.
* s = '12:01:00 AM'
Return '00:01:00'.
내 풀이:
func timeConversion(s: String) -> String {
// Foundation에 dateFormatter 있으므로 활용
var timeFormatter = DateFormatter()
timeFormatter.dateFormat = "h:mm:ssa"
// Date 타입으로 변경
let date = timeFormatter.date(from: s)
// AM PM 없애기 위해 새로운 format 적용
timeFormatter.dateFormat = "HH:mm:ss"
// 변경된 format 적용된 date의 문자열 반환
return timeFormatter.string(from: date!)
}
회고:
Foundation 프레임워크에 DateFormatter가 있다는 것을 인지하고 있어, 간단하게 풀 수 있었다.
728x90
'Algorithm > HackerRank' 카테고리의 다른 글
[Implementation] Apple and Orange (0) | 2023.05.12 |
---|---|
[Implementation] Grading Students (0) | 2023.05.12 |
[Warm Up] Birthday Cake Candles (0) | 2023.05.11 |
[Warm Up] Staircase (0) | 2023.05.11 |
[Warm Up] Diagonal Difference (0) | 2023.05.11 |