题目描述:
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].
题目大意:
给定一个整数,返回其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'
本文链接:https://bookshadow.com/weblog/2017/02/12/leetcode-base-7/
请尊重作者的劳动成果,转载请注明出处!书影博客保留对文章的所有权利。
如果这篇博客对你有帮助,请支持书影博客
你的捐赠将用于服务器与域名费用,帮助免费技术内容持续更新。
支付宝
微信
建议金额:¥5 / ¥10 / ¥20 / ¥50(扫码后自行输入)