Seems `abs` in Python slows down everything.


  • 0
    L

    My original version beats 89% (32ms)

    class Solution(object):
        def convertToBase7(self, num):
            """
            :type num: int
            :rtype: str
            """
            if num == 0:
                return '0'
            sign = -1 if num < 0 else 1
            newNum = sign * num
            stack = []
            while newNum > 0:
                stack.append(str(newNum % 7))
                newNum = newNum // 7
            return '-'+''.join(stack[::-1]) if num < 0 else ''.join(stack[::-1])
    

    But when I used newNum = abs(num) instead of doing the sign * num, it immediately slowed down to 27% (42ms)


Log in to reply
 

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