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;
    }
};
相关推荐
To_OC35 分钟前
LC 131 分割回文串:刚学回溯时,我连怎么切字符串都想不明白
javascript·算法·leetcode
炸膛坦客2 小时前
单片机/C/C++八股:(二十六)IIC 专题(I²C)---- 上集
c语言·c++·单片机
旖-旎2 小时前
LeetCode 518:零钱兑换||(完全背包)—— 题解
c++·算法·leetcode·动态规划·背包问题
To_OC2 小时前
LC 42 接雨水:暴力超时卡半天?前后缀数组一用就通了
javascript·算法·leetcode
Henry Zhu1232 小时前
C++中的特殊成员函数与智能指针
c++
_wyt0015 小时前
多重背包问题详解
c++·背包dp
Ljwuhe5 小时前
C++——多态
开发语言·c++
越甲八千6 小时前
STL stack为何没有迭代器
c++
从零开始的代码生活_8 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
云小逸8 小时前
【C++ 第七阶段:模板、泛型编程与工程综合详解】
开发语言·c++