Leetcode #504 Base 7

来源:互联网 发布:农民起义 成功 知乎 编辑:程序博客网 时间:2024/06/10 02:48

Description

Given an integer, return its base 7 string representation.

Note

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

Example

Example 1:
Input: 100
Output: “202”

Example 2:
Input: -7
Output: “-10”

Code

class Solution(object):    def convertToBase7(self, num):        """        :type num: int        :rtype: str        """        flag = 0        if num == 0:            return "0"        if num < 0:            flag = 1            num = -num        res = ""        while num:            res = str(num % 7) + res            num /= 7        return "-" + res if flag else res
0 0
原创粉丝点击