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. MacTex TexStudio Configuration 配置

    在Mac上使用Latex的话主流是安装MacTex,对于IDE的选择有很多,像什么自带的TexShop,或者是TexStudio,Latexian,Texpad,Texmaker等,甚至可以直接使用一 ...

  2. java封装实现Excel建表读写操作

    对 Excel 进行读写操作是生产环境下常见的业务,网上搜索的实现方式都是基于POI和JXL第三方框架,但都不是很全面.小编由于这两天刚好需要用到,于是就参考手写了一个封装操作工具,基本涵盖了Exce ...

  3. day11 十一、函数对象,名称空间,作用域,和闭包

    一.函数对象 1.函数对象:函数名存放的就是函数的地址,所以函数名也是对象,称之为函数对象 a = 10 print(a,id(a)) def fn(): num = 10 print('fn fuc ...

  4. poj2728 Desert King【最优比率生成树】【Prim】【0/1分数规划】

    含[最小生成树Prim]模板. Prim复杂度为$O(n^2),适用于稠密图,特别是完全图的最小生成树的求解.   Desert King Time Limit: 3000MS   Memory Li ...

  5. MFC实现一元稀疏多项式运算器

    MFC实现一元稀疏多项式运算器 基本要求 输入并建立两个多项式 多项式a与b相加,建立和多项式c 多项式a与b相减,建立差多项式d 输出多项式a, b, c, d.输出格式:比如多项式a为:A(x)= ...

  6. [No0000153]详解C# 迭代器【转】

    迭代器模式是设计模式中行为模式(behavioral pattern)的一个例子,他是一种简化对象间通讯的模式,也是一种非常容易理解和使用的模式.简单来说,迭代器模式使得你能够获取到序列中的所有元素而 ...

  7. 【每日一题】 UVA - 340 阅读理解+模拟

    https://cn.vjudge.net/problem/UVA-340 题目很难读,差不多读了两天 意思是给你一个n个数的数列,然后有m个询问,每个询问也是一个n个数的数列,让你输出两个数:一个是 ...

  8. deepin中idea中文乱码解决

    打开终端 命令: sudo su 输入密码: aptitude search uming 显示: root@terwer-PC:/home/terwer# aptitude search uming ...

  9. English class 82 The Importance of traveling

    1,since I could basically walk : pretty much 2,Malaysian girl 3,profound love: deep very inportant 4 ...

  10. [development][profile][dpdk] KK程序性能调优

    KK程序: 1. 两个线程,第一个从DPDK收包,通过一个ring数据传递给第二个线程.第二个线程将数据写入共享内存. 2. 第二个内存在发现共享内存已满时,会直接丢弃数据. 3. 线程二有个选项de ...