LeetCode C++ 504. Base 7【Math】简单
发布日期:2021-07-01 02:52:17
浏览次数:3
分类:技术文章
本文共 759 字,大约阅读时间需要 2 分钟。
Given an integer, return its base 7 string representation.
Example 1:
Input: 100Output: "202"
Example 2:
Input: -7Output: "-10"
Note: The input will be in range of [-1e7, 1e7]
.
题意:给定一个整数,将其转化为 7
进制,并以字符串形式输出。
思路
进制转换。十进制到七进制的转换,很简单的题目,只是需要注意负数的处理。代码如下:
class Solution { public: string convertToBase7(int num) { string ans; bool minus = false; if (num < 0) { num = -num; minus = true; } do { int t = num % 7; num /= 7; ans.push_back('0' + t); } while (num); if (minus) ans.push_back('-'); reverse(ans.begin(), ans.end()); return ans; }};
效率如下:
执行用时:0 ms, 在所有 C++ 提交中击败了100.00% 的用户内存消耗:6.1 MB, 在所有 C++ 提交中击败了11.93% 的用户
转载地址:https://memcpy0.blog.csdn.net/article/details/108806972 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2024年04月15日 01时42分15秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
ubuntu sublime无法输入中文问题
2019-05-03
启用fcitx-qimpanel面板程序
2019-05-03
nohub 管理后台任务
2019-05-03
[备忘] git http(https) 免密码提交
2019-05-03
proxy bash
2019-05-03
【备忘】nginx 配置 某个路径代理
2019-05-03
【备忘】 git log
2019-05-03
【备忘】chrome插件
2019-05-03
浅谈Q的基本实现
2019-05-03
[备忘] 命令行连接无线网 linux
2019-05-03
备忘 socks 转http
2019-05-03
visual studio code使用------ 代码提示
2019-05-03
[备忘] linux 有用却不常用命令
2019-05-03
基本的http压力测试工具
2019-05-03
微信开发公众号本地调试
2019-05-03
[备忘] redis 生产环境配置
2019-05-03
[备忘] linux 有用却不常用命令
2019-05-03
git 自动部署
2019-05-03
Webstorm 快捷键
2019-05-03
[备忘] [坑坑坑!]windows tar 压缩文件夹到指定目录下面
2019-05-03