Leetcode - 504. Base 7 (位运算)

Given an integer, return its base 7 string representation.

Example 1:

Input: 100
Output: "202"

Example 2:

Input: -7
Output: "-10"

Note: The input will be in range of [-1e7, 1e7].

def convertToBase(num):

    sign = '' if num >= 0 else '-'

    nums = abs(num)

    ans = 0

    while num >= 7:
        res,mod = divmod(num,7)
        ans.append(str(res))

    ans.append(str(num))

    return  sign + ''.join(reversed(ans))

 

上一篇:504. Base 7 (7进制)


下一篇:Kubernetes Ingress诡异的502、503、504等奇葩问题(一)