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