[LeetCode] 504. Base 7_Easy tag: Math
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的更多相关文章
- 45. leetcode 504. Base 7
504. Base 7 Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: ...
- [LeetCode] 504. Base 7 基数七
Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...
- [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. ...
- [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 ...
- LeetCode 504. Base 7 (C++)
题目: Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "2 ...
- [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 ...
- [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 ...
- C#版 - Leetcode 504. 七进制数 - 题解
C#版 - Leetcode 504. 七进制数 - 题解 Leetcode 504. Base 7 在线提交: https://leetcode.com/problems/base-7/ 题目描述 ...
- 【leetcode】504. Base 7
problem 504. Base 7 solution: class Solution { public: string convertToBase7(int num) { ) "; st ...
随机推荐
- 《Thinkphp5使用Socket服务》 入门篇
上车啦!!! 今天来说一下thinkphp5.0下使用wokerman的socket服务. 安装: composer的安装方法,Windows下直接下个composer的应用程序,双击安装,环境变量同 ...
- 使用 PREPARE 的几个注意点
简单的用set或者declare语句定义变量,然后直接作为sql的表名是不行的,mysql会把变量名当作表名.在其他的sql数据库中也是如此,mssql的解决方法是将整条sql语句作为变量,其中穿插变 ...
- 启动elk中elasticsearch服务报错which: no java in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)
解决办法: vi /etc/sysconfig/elasticsearch JAVA_HOME=/usr/local/java sudo systemctl restart elasticsearch ...
- [No0000118]SQL Server附加数据库拒绝访问解决方法汇总
修改权限 打开要附加的数据库文件所在的文件夹,右键单击mdf文件,选择“属性”: 单击“安全”选项卡,给所有用户添加读写权限. 最后点击"确定"就可以了.修改权限完成后,你就可以成 ...
- [No0000D2]ClearCSharp编程清理脚本批处理bat
for /f "tokens=*" %%a in ('dir obj /b /ad /s ^|sort') do rd "%%a" /s/q for /f &q ...
- [No0000C5]VS2010删除空行
VS2010删除空行,查找内容:^:b*$\n,替换为:,查找范围:当前文档,使用:正则表达式
- SQL Fundamentals || Oracle SQL语言
对于SQL语言,有两个组成部分: DML(data manipulation language) 它们是SELECT.UPDATE.INSERT.DELETE,就象它的名字一样,这4条命令是用来对数据 ...
- ROADS POJ - 1724 约束最短路 暴搜 加剪枝
http://poj.org/problem?id=1724 题意:最短路的模板,不过每条边加上一个费用,要求总费用不超过k 题解:不能用dijkstra ,直接暴力,dfs维护len和cost. 普 ...
- SpringMVC(三):参数绑定、输入输出转换
一.参数解析绑定 1. 自定义绑定:不绑定某些项 @InitBinder private void initBinder(WebDataBinder dataBinder) { dataBinder. ...
- oracle sqlplus login.sql设置
sqlplus在启动时会自动运行两个脚本:glogin.sql.login.sql这两个文件 执行顺序为 1.默认在在$ORACLE_HOME/sqlplus/admin路径下查找glogin.sql ...