👨🏻‍💻알고리즘/프로그래머스

[프로그래머스/Kotlin] Lv1. 자릿수 더하기

waveofmymind 2023. 3. 14. 00:31

문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/12931?language=kotlin 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

코드

class Solution {
    fun solution(n: Int): Int {
        var answer = 0
        n.toString().map {
            answer += it.digitToInt()
        }
        return answer
    }
}

풀이

  • 코틀린에서 char 타입을 int 타입으로 변환하는 toInt() 메서드는 deprecated 되었다.
  • 그래서 찾아보니 digitToInt() 메서드를 새로 지원하여 사용했다.