Base 7 Problem
Description
LeetCode Problem 504.
Given an integer num, return a string of its base 7 representation.
Example 1:
1
2
Input: num = 100
Output: "202"
Example 2:
1
2
Input: num = -7
Output: "-10"
Constraints:
- -10^7 <= num <= 10^7
Sample C++ Code
1
2
3
4
5
6
7
8
class Solution {
public:
string convertToBase7(int n) {
if (n < 0) return "-" + convertToBase7(-n);
if (n < 7) return to_string(n);
return convertToBase7(n / 7) + to_string(n % 7);
}
};