Interview Prep!!
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem is asking to convert a given integer to its base 7 representation. My first
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is asking to convert a given integer to its base 7 representation. My first thought is to repeatedly divide the number by 7 and collect the remainders, which would give the digits of the base 7 number in reverse order. If the number is negative, we handle it separately by converting the absolute value and then prepending a negative sign to the result.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Initial Check**: If the input number is 0, return "0" immediately as the base 7 representation of 0 is simply "0".\n2. **Negative Number Handling**: If the number is negative, convert it to its absolute value and set a flag `f` to indicate that it was negative.\n3. **Digit Collection**: Use a `while` loop to repeatedly divide the number by 7 and collect the remainders. These remainders represent the digits of the base 7 number but in reverse order.\n4. **Reverse the String**: Convert the collected digits into a string and reverse it to get the correct order.\n5. **Negative Sign Handling**: If the original number was negative, prepend a \'-\' to the result.\n6. **Return the Result**: Return the final base 7 representation.\n\n# Complexity\n- **Time complexity**: \n  The time complexity is $$O(\\log_7(\\text{num}))$$ because the loop iterates for the number of digits in the base 7 representation, which is proportional to the logarithm of the number to the base 7.\n\n- **Space complexity**:\n  The space complexity is also $$O(\\log_7(\\text{num}))$$ due to the space required to store the digits in the `StringBuilder`.\n\n# Code\n```java\nclass Solution {\n    public String convertToBase7(int num) {\n        if(num == 0){\n            return "0";\n        }\n        \n        int f = 0;\n        StringBuilder sb = new StringBuilder();\n        \n        if(num < 0){\n            f = 1;\n            num = -num;\n        }\n        \n        while(num > 0){\n            sb.append(num % 7);\n            num /= 7;\n        }\n        \n        String ori = sb.reverse().toString();\n        \n        if(f == 1){\n            return "-" + ori;\n        }\n        \n        return ori;\n    }\n}\n\n```\n![upvote_meme.PNG](https://assets.leetcode.com/users/images/15cb05c5-a8f9-4fed-8af6-2e661cd07ca5_1725891823.750655.png)\n\n
['Math', 'Java']