其他分享
首页 > 其他分享> > Leetcode - 504. Base 7 (位运算)

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))

 

标签:num,sign,Base,1e7,ans,Input,504,Leetcode,append
来源: https://blog.csdn.net/weixin_41362649/article/details/99986809