Base 7 LeetCode solution in C++
1Method 1 : - -- -- -- -- -- -- -- -class Solution {2public:3string convertToBase7(int num) {4if (num < 0)5return "-" + convertToBase7(-num);6else if (num < 7)7return to_string(num);8else9return convertToBase7(num / 7) + convertToBase7(num % 7);10}11};1213Method 2 : - -- -- -- -- -- -- -- -class Solution {14public:15string convertToBase7(int num) {16string res = "";17int num1 = num;18if (num == 0) return "0";19num = abs(num);20while (num > 0) {21res += to_string(num % 7);22num = num / 7;23}24reverse(res.begin(), res.end());25if (num1 < 0) res = "-" + res;26return res;27}28};