Leetcode 504 Solution

This article provides solution to leetcode question 504 (base-7)

https://leetcode.com/problems/base-7

Solution

class Solution {
public:
    string convertToBase7(int num) {
        if (num == 0)
            return "0";
        else if (num < 0)
            return "-" + convertToBase7(-num);

        string res;

        while (num)
        {
            res += (num % 7) + '0';
            num /= 7;
        }

        reverse(res.begin(), res.end());
        return res;
    }
};