Leetcode学习(12)—— Base 7

来源:互联网 发布:argentum银霜 知乎 编辑:程序博客网 时间:2024/05/22 14:42

Given an integer, return its base 7 string representation.

Example 1:Input: 100Output: "202"Example 2:Input: -7Output: "-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]

这里写图片描述

0 0
原创粉丝点击