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
- Problem Statement
- Complexity Analysis
- Base solution in C++
- Base solution in Java
- Base solution in Python
- Additional Resources

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
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.