题目 Given an integer, return its base 7 string representation. Example 1: 12 Input: 100Output: "202" Example 2: 12 Input: -7Output: "-10" Note: The input will be in range of [-1e7, 1e7]. 难度 Easy 方法 对num取除以7的余数,即为最低位的数,然后将num/7赋值给num,继续取num除以7的余数即为倒数第二位的数,依次类推。注意num为0时需要返回"0" python代码 12345678910111213141516171819202122 class Solution(object): def convertToBase7(self, num): """ :type num: int :rtype: str """ if num == 0: return "0" symbol = "" if num < 0: num = 0 - num symbol = "-" result = "" while num > 0: result += str(num % 7) num = num / 7 return symbol + result[::-1]assert Solution().convertToBase7(100) == "202"assert Solution().convertToBase7(-7) == "-10" 赞微海报分享
近期评论