LeetCode Solutions

504. Base 7

Time: $O(\log_7 n)$

Space: $O(1)$

			

class Solution {
 public:
  string convertToBase7(int num) {
    if (num < 0)
      return "-" + convertToBase7(-num);
    if (num < 7)
      return to_string(num);
    return convertToBase7(num / 7) + to_string(num % 7);
  }
};
			

class Solution {
  public String convertToBase7(int num) {
    if (num < 0)
      return "-" + convertToBase7(-num);
    if (num < 7)
      return String.valueOf(num);
    return convertToBase7(num / 7) + String.valueOf(num % 7);
  }
}