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, 然后把余数放到ans里面, 最后reverse ans加上符号即可.

Code

class Solution:
def convertToBase7(self, num):
if num == 0: return ''
pos = "-" if num < 0 else ""
num, ans = abs(num), ''
while num > 0:
rem, num = divmod(num, 7)
ans += str(num)
num = rem
return pos + ans[::-1]

[LeetCode] 504. Base 7_Easy tag: Math的更多相关文章

  1. 45. leetcode 504. Base 7

    504. Base 7 Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: ...

  2. [LeetCode] 504. Base 7 基数七

    Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...

  3. [LeetCode] 258. Add Digits_Easy tag: Math

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  4. [LeetCode] 441. Arranging Coins_Easy tag: Math

    You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...

  5. LeetCode 504. Base 7 (C++)

    题目: Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "2 ...

  6. [LeetCode] 292. Nim Game_Easy tag: Math

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  7. [LeetCode] 458. Poor Pigs_Easy tag: Math

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  8. C#版 - Leetcode 504. 七进制数 - 题解

    C#版 - Leetcode 504. 七进制数 - 题解 Leetcode 504. Base 7 在线提交: https://leetcode.com/problems/base-7/ 题目描述 ...

  9. 【leetcode】504. Base 7

    problem 504. Base 7 solution: class Solution { public: string convertToBase7(int num) { ) "; st ...

随机推荐

  1. C - Monthly Expense

    Farmer John is an astounding accounting wizard and has realized he might run out of money to run the ...

  2. python数据类型之字典(二)

    字典的基本操作 键值查找: >>> aInfo = {'Wangdachui':3000,'Niuyun':2000,'Linling':4500,'Tianqi':8000} &g ...

  3. C语言复制图片文件

    以下代码将文件一的图片复制到文件二中 #include<stdio.h> #include<stdlib.h> int main() { char ch; char fname ...

  4. DevOps平台的“精益创业”之路

    本文内容节选自第六届全球软件案例研究峰会,时任中国移动通信集团浙江有限公司罗琼老师,申健老师分享的<DevOps平台的“精益创业”之路>实录,重点分享:DevOps产品研发过程,对外实施敏 ...

  5. vins-mono代码解读

    系统框架介绍 1. Measurement Preprocessing(观测预处理):对图像提feature做feature tracking,输出tracked feature list, 对IMU ...

  6. arcgisengine实现矩形转面

    面文件都有几何类型. arcengine在绘图时,不规则的多边形的几何类型是esriGeometryPolygon,矩形的几何类型是esriGeometryEnvelope,圆的几何类型是esriGe ...

  7. Java的四种内部类(含代码实例)

    写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...

  8. idea找不到import project

    一.首先File->close project 关完后,在界面你就可以看到import project

  9. project proposal写作框架

    主要有八个因素: 背景(Your Background):对于proposal有意义的要点,如国家职业证书.技能.经验.能力和实习经历等. 大纲(Outline Proposal):描述你明确的感兴趣 ...

  10. oracle中varchar2(2)存不了一个汉字的原因

    错误提示: 一个汉字占了三个字节,而不是两个,这跟字符集有关. 查一下字符集:select userenv('language') from dual; 结果显示,本机Oracle的字符集是UTF-8 ...