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;
    }
};
相关推荐
熊猫_豆豆2 小时前
QT6 Android C++ 自制美观闹钟
android·c++·qt·闹钟
YuK.W3 小时前
Leetcode100: 70.爬楼梯、118.杨辉三角、198.打家劫舍
java·算法·leetcode
随意起个昵称3 小时前
状压dp-基础题目2([USACO12MAR] Cows in a Skyscraper G)
c++·算法·动态规划
无限的鲜花4 小时前
协程本质是函数加状态机——零基础深入浅出 C++20 协程
c++·算法·c++20
精明的身影4 小时前
C++自学之路1:Hello world
开发语言·c++
旖-旎4 小时前
《LeetCode 64 最小路径和 || LeetCode 174 地下城游戏》
c++·算法·leetcode·动态规划
森林古猿15 小时前
再论斜率优化
c++·学习·算法
水利行业RTU手艺人5 小时前
RTU固件中的“数据保险箱”——离线数据补发系统设计
c++·stm32·单片机·物联网
凯瑟琳.奥古斯特6 小时前
力扣1009补码解法C++实现
开发语言·c++·算法·leetcode·职场和发展
zh_xuan8 小时前
c++ optional用法
开发语言·c++·optional