[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 ...
随机推荐
- 移动端H5混合开发,Touch触控,拖拽,长按, 滑屏 实现方案
概述 近期由于产品快速原型开发的需要,不想用原声的方式开发App两端一起搞时间来不及,目前产品处于大量上feature的阶段,采用混合开发是最合适的选择,所以花了3天的时间研究怎么去实现移动端,拖拽, ...
- 【Linux】解决Android Stadio报错:error in opening zip file
报错: Failed to complete Gradle Execution Cause: error in opening zip file. 原因: 安装gradle失败引起的,往往是上网需要验 ...
- Saltstack设置安装源为阿里源
Saltstack设置安装源为官方源有时候在国内网络不好安装较慢或者安装不上,可设置为阿里源 比如对于 Centos 7 系统,在 saltstack 的官网提供的配置初始化手册是: sudo yum ...
- HDU 1166 - 敌兵布阵 - [分块]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题解: 本题作为一个模板题,我用它来检验过总查询时间复杂度为 $O(q \log n)$ 的树状 ...
- [No0000192]Vim打开和保存文件-Vim使用技巧(7)
使用Vim打开和保存文件是最常用的操作,介绍使用edit命令通过文件路径来打开文件,使用write命令保存文件,当文件路径不存在或用户权限不匹配时,使用write命令调用外部shell程序完成操作. ...
- WebStorm中配置node.js(Windows)
WebStorm中配置node.js(Windows) 一.node 1.下载安装包 32 位 : https://nodejs.org/dist/v4.4.3/node-v4.4.3-x86.msi ...
- 自动化运维工具-pssh工具安装配置及简单使用讲解
1.先决条件:安装pssh工具要求python版本大于2.4即可. 安装pssh工具的主机针对远程主机需要配置免秘钥认证: ssh-keygen -t rsa ssh-copy-id [remoteh ...
- haproxy实现会话保持
HAProxy系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html 1.反向代理为什么需要设置cookie 任何一个七层的http负载均衡器,都 ...
- 2015年蓝桥杯省赛A组c++第3题
/* 小明发现了一个奇妙的数字.它的平方和立方正好把0~9的10个数字每个用且只用了一次. 你能猜出这个数字是多少吗? 请填写该数字,不要填写任何多余的内容. */ #include<cstdi ...
- 2018/09/17《涂抹MySQL》【性能优化及诊断】学习笔记(七)
读 第十三章<MySQL的性能优化与诊断> 总结 一说性能优化,整个人都像被打了鸡血一样