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的更多相关文章

  1. [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  2. [LeetCode&Python] Problem 387. First Unique Character in a String

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  3. [LeetCode&Python] Problem 427. Construct Quad Tree

    We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...

  4. [LeetCode&Python] Problem 371. Sum of Two Integers

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  5. [LeetCode&Python] Problem 520. Detect Capital

    Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...

  6. [LeetCode&Python] Problem 226. Invert Binary Tree

    Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Tr ...

  7. [LeetCode&Python] Problem 905: Sort Array By Parity

    Given an array A of non-negative integers, return an array consisting of all the even elements of A, ...

  8. [LeetCode&Python] Problem 1: Two Sum

    Problem Description: Given an array of integers, return indices of the two numbers such that they ad ...

  9. [LeetCode&Python] Problem 682. Baseball Game

    You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...

随机推荐

  1. javascript监听按键

    document.addEventListener('keydown',test); function test(e){ var x=e.keyCode; if(x == 49){ console.l ...

  2. 20175312 2018-2019-2 《Java程序设计》第4周学习总结

    20175312 2018-2019-2 <Java程序设计>第4周学习总结 教材学习内容总结 已依照蓝墨云班课的要求完成了第五章的学习,主要的学习渠道是PPT,和书的课后习题. 总结如下 ...

  3. linux基础11-bash编程(字符串测试 和 for循环)

    练习:传递一个用户名参数给脚本,判断此用户的用户名跟其基本组的组名是否一致,并将结果显示出来.(1)字符测试:==:测试是否相等,相等为真,不等为假!=: 测试是否不等,不等为真,等为假>< ...

  4. 随机森林和GBDT

    1. 随机森林 Random Forest(随机森林)是Bagging的扩展变体,它在以决策树 为基学习器构建Bagging集成的基础上,进一步在决策树的训练过程中引入了随机特征选择,因此可以概括RF ...

  5. postman(一):主界面模块解析

    在做接口测试时经常会用到postman,但是一直没有总结过,太过零散,这次找了一些好的资料,结合自己平时所用到的功能,总结一波 打开postman,主界面如下 左侧菜单 1.History标签 里面存 ...

  6. 音乐推荐与Audioscrobbler数据集

    1. Audioscrobbler数据集 数据下载地址: http://www.iro.umontreal.ca/~lisa/datasets/profiledata_06-May-2005.tar. ...

  7. Oracle数据库联机重定义讲解及错误处理

    1.1. 关键字:联机重定义/SYNC_INTERIM_TABLE/GATHER_TABLE_STATS 1.2. 需求:数据表的清理机制需要优化 离线消息表采用delete的方式定期对过期的数据进行 ...

  8. python--接口开发

    一.接口开发需要用到flask类1.首先安装flask类:cmd--pip install flask2.导入flask类:import flask3.以下是用一个例子来说明: import flas ...

  9. Dropout, DropConnect ——一个对输出,一个对输入

    Deep learning: Dropout, DropConnect from:https://www.jianshu.com/p/b349c4c82da3 Dropout 训练神经网络模型时,如果 ...

  10. Nearest Common Ancestors (POJ 1330)

    A rooted tree is a well-known data structure in computer science and engineering. An example is show ...