[LeetCode&Python] Problem 504. Base 7

Posted chiyeung

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode&Python] Problem 504. Base 7相关的知识,希望对你有一定的参考价值。

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):
        """
        :type num: int
        :rtype: str
        """
        ans=""
        if num<0:
            neg=True
        else:
            neg=False
        num=abs(num)
        while num>=7:
            ans+=str(num%7)
            num=num//7
        ans+=str(num)
        if neg:
            ans=‘-‘+ans[::-1]
            return ans
        return ans[::-1]

  

以上是关于[LeetCode&Python] Problem 504. Base 7的主要内容,如果未能解决你的问题,请参考以下文章

C++&Python描述 LeetCode C++&Python描述 LeetCode 剑指 Offer 22. 链表中倒数第k个节点

[LeetCode&Python] Problem 202. Happy Number

[LeetCode&Python] Problem 520. Detect Capital

[LeetCode&Python] Problem 383. Ransom Note

[LeetCode&Python] Problem 458. Poor Pigs

[LeetCode&Python] Problem 682. Baseball Game