Problem Solving

23. Add Digits

굥깡 2022. 9. 13. 08:59
728x90

https://leetcode.com/problems/add-digits/

 

Add Digits - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

한 자릿수가 될 때까지 각각의 자릿수를 계속 더하는 문제

class Solution:
    def addDigits(self, num: int) -> int:
        digits = str(num)
        add = 0
        while True:
            add = 0
            for i in digits:
                add = add + int(i)
            digits = str(add)
            if len(str(add)) == 1:
                break
        return add

반복문 두 개 써서 submit이 안 될 줄 알았음 하하