C++ | Leetcode C++题解之第504题七进制数

题目:

题解:

cpp 复制代码
class Solution {
public:
    string convertToBase7(int num) {
        if (num == 0) {
            return "0";
        }
        bool negative = num < 0;
        num = abs(num);
        string digits;
        while (num > 0) {
            digits.push_back(num % 7 + '0');
            num /= 7;
        }
        if (negative) {
            digits.push_back('-');
        }
        reverse(digits.begin(), digits.end());
        return digits;
    }
};
相关推荐
Larry_Yanan4 小时前
QML学习笔记(四十)QML的ApplicationWindow和StackView
c++·笔记·qt·学习·ui
Miraitowa_cheems5 小时前
LeetCode算法日记 - Day 73: 最小路径和、地下城游戏
数据结构·算法·leetcode·职场和发展·深度优先·动态规划·推荐算法
野蛮人6号5 小时前
力扣热题100道之560和位K的子数组
数据结构·算法·leetcode
Swift社区6 小时前
LeetCode 400 - 第 N 位数字
算法·leetcode·职场和发展
Kratzdisteln6 小时前
【C语言】Dev-C++如何编译C语言程序?从安装到运行一步到位
c语言·c++
剪一朵云爱着7 小时前
力扣2080. 区间内查询数字的频率
算法·leetcode
Dream it possible!8 小时前
LeetCode 面试经典 150_栈_有效的括号(52_20_C++_简单)(栈+哈希表)
c++·leetcode·面试··哈希表
kyle~8 小时前
C++--- override 关键字 强制编译器验证当前函数是否重写基类的虚函数
java·前端·c++
HY小海8 小时前
【C++】AVL树实现
开发语言·数据结构·c++
仰泳的熊猫8 小时前
LeetCode:701. 二叉搜索树中的插入操作
数据结构·c++·算法·leetcode