My Java solution without reverse()


  • 0
    public class Solution {
        public String convertToBase7(int num) {
            if(num == 0) return "0";
            boolean isNagative = true;
            if(num >= 0) isNagative = false;
            num = Math.abs(num);
            int [] base7 = {1, 7, 49, 343, 2401, 16807, 117649, 823543, 5764801, 40353607, 282475249};
            StringBuilder result = new StringBuilder(11); 
            int i = 10;
            for(; i >= 0; i--) {
                int digit = num / base7[i];
                result.append(digit);
                num -= digit * base7[i];
            } 
            while(result.charAt(++i) == '0');
            return isNagative ? "-" + result.substring(i) : result.substring(i);
        }
    }
    

Log in to reply
 

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