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].

Solution in python:

class Solution:
    def convertToBase7(self, num: int) -> str:
        if not num:
            return "0"
        result = ""
        flag = 1 if num >= 0 else -1
        num *= flag
        while num > 0:
            num, r = divmod(num, 7)
            result = str(r) + result
        if flag == -1:
            result = "-" + result
        return result
最后修改日期: 2021年2月1日

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。