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;
    }
};
相关推荐
j7~5 小时前
【C++】《C++STL 笔记:搞懂序列 / 关联容器,聊聊 set 和 map》
c++·set·map·multimap·multiset·键值对pair·序列是容器和关联式容器
郝学胜-神的一滴6 小时前
《C++11 工程级应用01:告别冗长类型,开启简洁高效编码新时代》深度解读
开发语言·c++·算法·软件开发·系统设计
Navigator_Z13 小时前
LeetCode //C - 1206. Design Skiplist
c语言·算法·leetcode
码行山野赴时序归途13 小时前
三道经典数组题:从暴力到最优的算法思维
c语言·开发语言·数据结构·算法·leetcode
Navigator_Z15 小时前
LeetCode //C - 1209. Remove All Adjacent Duplicates in String II
c语言·算法·leetcode
码匠许师傅16 小时前
【C++ 面试真题】30. 聊聊 C++ 的互斥锁与读写锁
java·c++·面试
ShineWinsu17 小时前
对于 C++:C++14中从变量模板、泛型 Lambda 到并发与字面量的解析
c++·算法
土司大王17 小时前
LeetCode hot100——合并两个有序链表
算法·leetcode·链表
一木 之林19 小时前
四、STL容器与数据结构
开发语言·数据结构·c++
程与留19 小时前
07_QMainWindow 与应用程序框架——菜单栏、工具栏、状态栏
c++·qt