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
留言