[JAVA] Divide 7 / T : O(N), S : O(N), N:digit number


  • 0
    J
    class Solution {
        public String convertToBase7(int num) {
            boolean minus = false;
            if(num < 0){
                num *= -1;
                minus = true;
            }
            else if(num == 0)
                return "0";
                
            StringBuffer strbuf = new StringBuffer();
            while(num > 0){
                strbuf.insert(0, num%7);
                num = num/7;
            }
            
            if(minus){
                strbuf.insert(0, "-");
            }
            return strbuf.toString();
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.