Best solution for Base Conversion
Intuition\n Describe your first thoughts on how to solve this problem. \nTo convert an integer to base 7, I need to repeatedly divide the number by 7, collectin
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo convert an integer to base 7, I need to repeatedly divide the number by 7, collecting the remainders. These remainders represent the digits in the new base, starting from the least significant digit.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Handle Negative Numbers:** Since the output should be a string, check if the number is negative. If it is, work with its absolute value and append a "-" at the end.\n2. **Base Conversion:** Use a loop to divide the number by 7, storing the remainders. The remainders give the digits of the base 7 representation.\n3. **Construct the Result:** Since the remainders are collected in reverse order, reverse the string before returning it.\n\n# Complexity\n- Time complexity: O(log7n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(log7n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public String convertToBase7(int num) {\n        return baseX(num,7);\n    }\n    private String baseX(int num,int base){\n        if(num==0) return "0";\n        StringBuilder sb = new StringBuilder();\n        int temp = num;\n        num = Math.abs(num);\n        while(num>=base){\n            int rem = num%base;\n            sb.append(rem);\n            num /= base;\n        }\n        sb.append(num);\n        if(temp<0)\n            sb.append("-");\n        sb.reverse();\n        return sb.toString();\n    }\n}\n```
['Java']