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;
    }
};
相关推荐
melonbo8 分钟前
桥接模式C++
开发语言·c++·设计模式·桥接模式
现在,此刻8 小时前
leetcode 11. 盛最多水的容器 -java
java·算法·leetcode
_多拉不懂A梦9 小时前
FFmepg源码系列-avformat_open_input()
c++·ffmpeg·音视频
码达拉10 小时前
顺序表的总结及模拟实现
数据结构·c++
源远流长jerry10 小时前
OpenHarmony概述与使用
c语言·c++·鸿蒙系统
艾莉丝努力练剑10 小时前
深入详解C语言的循环结构:while循环、do-while循环、for循环,结合实例,讲透C语言的循环结构
c语言·开发语言·c++·学习
火车叨位去194911 小时前
力扣top100(day01-05)--矩阵
算法·leetcode·矩阵
冬夜戏雪13 小时前
java学习 leetcode 二分查找 图论
java·学习·leetcode
火车叨位去194913 小时前
力扣top100(day02-05)--二叉树 02
算法·leetcode·职场和发展
James. 常德 student14 小时前
leetcode-hot-100 (图论)
算法·leetcode·图论