Can someone tell me how it can be faster in python?


  • 0
    Y

    My code is as following:

    def addDigits(self, num):
        """
        :type num: int
        :rtype: int
        """
        return ((num-1)%9+1)*(num>0)
    

    Can someone tell me how it can be further optimized? I'm asking this because there are still a lot of Python submissions faster than this.


  • 1
    M
    def add_digits(num):
        x = num % 9
        if not x and num:
            return 9
        else:
            return x

Log in to reply
 

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