Swift | programmers/Lv1.

[Swift] LV.1 자연수 뒤집어 배열로 만들기

iOSDEv 2024. 2. 19. 10:47
문제

 

 

나의 풀이
func solution(_ n:Int64) -> [Int] {
    return String(n).reversed().compactMap { Int(String($0)) }
}

 

 

다른 사람 풀이
func solution(_ n:Int64) -> [Int] {
    return  "\(n)".compactMap { $0.hexDigitValue }.reversed()
}

 

func solution(_ n:Int64) -> [Int] {
    var num: Int = Int(n)
    var arr: [Int] = []

    while num > 0 {
        arr.append(num % 10)
        num /= 10
    }
    return arr
}

 

func solution(_ n:Int64) -> [Int] {
    return String(n).reversed().map { Int(String($0)) ?? 0 }
}

 

func solution(_ n:Int64) -> [Int] {
    var strArr = String(n).compactMap { $0.wholeNumberValue }

    return strArr.reversed()
}

 

  • wholeNumberValue?

 

 

"0", "1", "2" 같이 음이 ❌  정수를 표현한다면 wholeNumberValue 프로퍼티를 통해서 Int 로 변환해줄 수 있다!

 

 

 

wholeNumberValue | Apple Developer Documentation

The numeric value this character represents, if it represents a whole number.

developer.apple.com