Python without any loop/recursion in O(1) runtime


  • 0
    L

    Just figure out that any number divided by 9 then its remainder will be the min added digit.
    So the algorithm will be easy.

    class Solution(object):
        def addDigits(self, num):
            """
            :type num: int
            :rtype: int
            """
            if num < 10:
                return num
            
            if num % 9 == 0:
                return 9
            
            return num % 9
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.