1. 程式人生 > >Leetcode 504

Leetcode 504

給定一個整數,將其轉化為7進位制,並以字串形式輸出。

示例 1:

輸入: 100
輸出: "202"

示例 2:

輸入: -7
輸出: "-10"

注意: 輸入範圍是 [-1e7, 1e7] 。

class Solution(object):
    def convertToBase7(self, num):
        """
        :type num: int
        :rtype: str
        """
        temp = []
        s = ''
        if num == 0:
            return '0'
        if num > 0:
            while num > 0:
                s += str(num % 7)
                num = num / 7
            return s[::-1]
        else:
            num = abs(num)
            while num > 0:
                s += str(num % 7)
                num = num / 7
            return '-' + s[::-1]