Quantcast
Channel: 书影 - Entries for the tag leetcode
Viewing all articles
Browse latest Browse all 559

[LeetCode]Base 7

$
0
0

题目描述:

LeetCode 504. Base 7

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].

题目大意:

给定一个整数,返回其7进制的字符串。

注意: 输入在范围[-1e7, 1e7]之内。

解题思路:

进制转换

Python代码:

class Solution(object):
    def convertTo7(self, num):
        """
        :type num: int
        :rtype: str
        """
        n = abs(num)
        ans = ''
        while n:
            ans += str(n % 7)
            n /= 7
        return ['', '-'][num < 0] + ans[::-1] or '0'

 


Viewing all articles
Browse latest Browse all 559

Trending Articles