class Solution { public: string convertToBase7(int num) { if (num == 0) return "0"; long n = abs(num); int flag = num > 0 ? 1 : -1; string res; while (n) { res += to_string(n % 7); n /= 7; } if (flag == -1) res.push_back('-'); reverse(res.begin(), res.end()); return res; } };