[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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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, ...
- [LeetCode&Python] Problem 1: Two Sum
Problem Description: Given an array of integers, return indices of the two numbers such that they ad ...
- [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 ...
随机推荐
- spring mvc 的上传图片是怎么实现的?
spring mvc 的上传图片是怎么实现的? 导入jar包,commons-io.jar 及 commons-fileupload.jar 在springmvc的配置文件中配置Mutipart解析器 ...
- IP白名单的实现(PHP)
有些项目可能会用到一个IP地址的白名单黑名单之类的验证. 比如,只有IP地址在白名单中,才可以访问该系统. 那么此时,白名单的维护,一般是一个文件,里边是一些IP地址(每行一个IP),当然也有的可能是 ...
- UTF-8格式txt文件读取字节前三位问题
今天试着读取一份UTF-8格式的txt文件,内容如下 12345 但是每次读取之后转为String类型,输出字符串长度总是为6,并且第一位打印在控制台后不占任何空间. 经过debug查看字节码后发现, ...
- mysql ERROR 1045 和2058时(28000): 错误解决办法
mysql ERROR 1045 (28000): 错误解决办法 听语音 | 浏览:54286 | 更新:2018-02-23 14:34 | 标签:mysql 1 2 3 4 5 6 7 分步阅读 ...
- Redis 队列好处
Redis 队列好处 .降低流量高峰(并不是提升处理能力,系统的整体处理能力不变) .解除耦合(任务格式定好,各自演变,互不影响) .高可用(后台升级/崩溃完全不影响客户端的响应)
- Pandas 基础(10) - 用 Pivot table 做格式转换
Pivot allows you to transform or reshape data.Pivot 可以帮助我们改变数据的格式, 下面两个例子可以作为参考: 下面来看下具体实现, 首先引入一个 c ...
- C#反射详解
http://blog.csdn.net/educast/article/details/2894892(转) 两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏 ...
- SG函数值
如果只有单个游戏,只需找必胜态就行,不用找sg函数值,这样节省一个常数的时间. 但是多个游戏时一定要sg函数的异或来判断成败.因为虽然必败态一定到必胜态,但是必胜态不一定到必败态就是最优的.而单个游戏 ...
- 20165309 2017-2018-2《Java程序设计》课程总结
20165309 2017-2018-2<Java程序设计>课程总结 一.每周作业链接汇总 预备作业1:我期望的师生关系 预备作业2:技能学习经验与C语言 预备作业3:Linux安装及学习 ...
- [LeetCode]题15:3Sum
第一次解: res = [] nums.sort() if len(nums)<3:return [] for i in range(len(nums)-2): left = i+1 right ...