What's wrong with my code (python) ?


  • 0
    M

    Here is my code and could anyone tell me what's wrong with it? When input is 10, it returns 0 other than 1, thanks a lot.

    class Solution(object):

    def addDigits(self, num):
        """
        :type num: int
        :rtype: int
        """
        if num >= 0 and num < 10:
            total = num
        else:
            total = 0
            if num >0:
                total += num%10
                num = num/10
        return total

  • 0
    L

    ... so obviously you would get 0


  • 0
    C

    You do not have any recursion/loop, so apparently you did not sum all digit while you chose digit splitting method.
    Say when you split out "num // 10", you want to add it into total.


Log in to reply
 

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