Leetcode学习(12)—— Base 7

xiaoxiao2021-02-28  91

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): abs_num = abs(num) base_str = '' while abs_num >= 7: base_str += str(int(abs_num) % 7) abs_num /= 7 base_str += str(int(abs_num)) if num >= 0: return base_str[::-1] else: return '-' + base_str[::-1]

转载请注明原文地址: https://www.6miu.com/read-53903.html

最新回复(0)