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].
class Solution(object):
def convertToBase7(self, num):
"""
:type num: int
:rtype: str
"""
n = []
flag = False
if num<0:
flag = True
num = -num
while True:
n.append(str(num%7))
num = int(num/7)
if num ==0 :
break
if flag:
return '-' + ''.join(n[::-1])
return ''.join(n[::-1])
注意负号就可以