504. Base 7 LeetCode Solution

In this guide, you will get 504. Base 7 LeetCode Solution with the best time and space complexity. The solution to Base problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Base solution in C++
  4. Base solution in Java
  5. Base solution in Python
  6. Additional Resources
504. Base 7LeetCode Solution image

Problem Statement of Base

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

Example 1:
Input: num = 100
Output: “202”
Example 2:
Input: num = -7
Output: “-10”

Constraints:

-107 <= num <= 107

Complexity Analysis

  • Time Complexity: O(\log_7 n)
  • Space Complexity: O(1)

504. Base 7 LeetCode Solution in C++

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);
  }
};
/* code provided by PROGIEZ */

504. Base 7 LeetCode Solution in Java

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);
  }
}
// code provided by PROGIEZ

504. Base 7 LeetCode Solution in Python

N/A
 # code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.

See also  2926. Maximum Balanced Subsequence Sum LeetCode Solution