Categories:

Tags:



Problem

Given an integer num, return a string of its base 7 representation.

Constraints

  • -107 <= num <= 107

Solution

The problem Base 7 can be solved by simply performing a base 7 conversion.

Implementation

class Solution
{
  public:
    string convertToBase7(int num)
    {
        int conv = abs(num);

        string ret;
        do
        {
            ret += to_string(conv % 7);
        } while ((conv /= 7));

        if (num < 0)
            ret += '-';

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