504. Base 7(C++)
来源:互联网 发布:js prompt获取返回值 编辑:程序博客网 时间:2024/04/29 16:10
题目:
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进制数
解答:
class Solution {public: string convertToBase7(int num) { if (num == 0)//首先排除0存在的情况 return "0"; int n = abs(num);//减少运算量,先忽视负号 string s = ""; while (n > 0) { s = to_string(n % 7)+s; n/=7; } if (num < 0) s = "-" + s; return s; }};
改了几种书写方式以减少代码量,但看起来都不够直观,就这个吧
0 0
- 504. Base 7(C++)
- C base
- Leetcode-504. Base 7
- 【Leetcode】504. Base 7
- LeetCode 504. Base 7
- 504. Base 7
- [leetcode]504. Base 7
- 504. Base 7
- 504. Base 7
- 504. Base 7
- 504. Base 7
- LeetCode-504. Base 7
- 504. Base 7
- 504. Base 7
- 504. Base 7
- leetcode 504. Base 7
- 504. Base 7
- LeetCode:504. Base 7
- 111
- 容器跨主机通信之Calico
- sklearn简易安装教程
- 容器跨主机通信之flannel
- Struts2 S2-045 RCE Vulnerability Analysis & Brand New Exploit
- 504. Base 7(C++)
- 35个JAVA优化
- (使用通过混淆+自己第三方保留成功混淆)AndroidStudio 混淆打包
- (通用)Android App代码混淆终极解决方案
- Unity3D说明文档翻译-Searching
- gulp入门(15)- gulp-rev-append
- Unity说明文档翻译-Other Views
- gulp入门(16)- gulp-changed
- webstrom 多光标