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. nodejs的koa2框架

    官网文档 cnpm i --save-dev koa2 koa-router koa-body koa-static request npm install --save koa2 const koa ...

  2. sometimes we should use "disable fork" instead of "disable block_name"

    A disable named block statement stops the execution of all blocks with that same name in all threads ...

  3. Ubuntu启动时a start job is running for dev-disk-by延时解决

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

  4. iOS程序main函数之前发生了什么

    我是前言 一个iOS app的main()函数位于main.m中,这是我们熟知的程序入口.但对objc了解更多之后发现,程序在进入我们的main函数前已经执行了很多代码,比如熟知的+ load方法等. ...

  5. 转:手把手教你如何玩转Solr(包含项目实战)

    原文地址:手把手教你如何玩转Solr(包含项目实战) 参考原文

  6. Servlet重写init(ServletConfig config)还是init()

    原文地址:Servlet重写init(ServletConfig config)还是init() 写一个Servlet时,有时需要我们重写该Servlet的初始化方法,然后,究竟是重写init(Ser ...

  7. overridePendingTransition

    通过调用overridePendingTransition() 可以实时修改Activity的切换动画. 注意:该函数必须在Activity的onCreate()中调用或者finish()后立即调用.

  8. 【Python全栈-jQuery】jQuery基础知识

    前端学习之jQuery 一. jQuery是什么? <1> jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. < ...

  9. Vue项目

    1.新建Vue项目:vue init webpack projectName 2.vue-router模块 1.安装vue-router模块:npm install vue-router --save ...

  10. 安装Vue Devtools

    命令行进入vue-devtools\vue-devtools-master执行 cnpm install (貌似npm不太好使,也可能是我网络代理的原因) (淘宝镜像安装 npm install -- ...