Verbose Java Solution


  • 9

    Just keep dividing the current number by 7...

    public class Solution {
        public String convertTo7(int num) {
            if (num == 0) return "0";
            
            StringBuilder sb = new StringBuilder();
            boolean negative = false;
            
            if (num < 0) {
                negative = true;
            }
            while (num != 0) {
                sb.append(Math.abs(num % 7));
                num = num / 7;
            }
            
            if (negative) {
                sb.append("-");
            }
            
            return sb.reverse().toString();
        }
    }
    

  • 1
    D

    @shawngao It will do integer overflow for Integer.MIN_VALUE for num = -num


  • 1

    @deadman069 Fair point. Updated.


  • 0

    I have same idea with you.

    public class Solution {
        public String convertTo7(int num) {
            if(num == 0)return new String("0");
            boolean neg_sign = false;
            if(num<0)neg_sign = true;
            StringBuilder sb = new StringBuilder();
            num = Math.abs(num);
            while(num != 0){
                int tmp = num%7;
                sb.append(tmp);
                num /= 7;
            }
            if(neg_sign){
                sb.append('-');
            }
            sb.reverse();
            return sb.toString();
        }
    }
    

  • 1
    public String convertToBase7(int number) {
        if (number == 0) return "0";
        else {
            boolean isNegative = (number < 0);
            number = Math.abs(number);
            
            StringBuilder result = new StringBuilder();
            for(; number > 0; number /= 7)
                result.append(number % 7);		
            
            if (isNegative) 
                result.append("-");
            return result.reverse().toString();
        }
    }
    

  • 0
    J

    I would like to add one more line to judge if num is in (-7, 7). It will be a little bit quicker.

    public String convertToBase7(int num) {
            if (num == 0) return "0";
            if (num < 7 && num > 7 && num != 0) return Integer.toString(num);
            StringBuilder sb = new StringBuilder();
            boolean negative = false;
            if (num < 0) negative = true;
            while (num != 0) {
                sb.append(Math.abs(num % 7));
                num = num / 7;
            }
            if (negative) sb.append("-");
            return sb.reverse().toString();
        }
    

  • 0
    K

    Here is my code. Similiar solution:

    public String convertToBase7(int num) {
    	if (num ==0) return "0";
    	String res = "";
    	int absNum = Math.abs(num);
    	while (absNum!=0 ) {
    		res = absNum%7 + res;
    		absNum/=7;
    	}
    	return num>0?res:"-"+res;
    }
    

  • 0

    山哥这是我的solution,没有用StringBuilder,思路和您的非常相似 :-)

    public class Solution {
        public String convertToBase7(int num) {
            if (num == 0) return "0";
            
            int sign = (num > 0 ? 1 : -1);
            long res = 0;   // 因为base7会比base10的数字要长, 防止越界,必须用long
            int digit = 1;  // 位数,从个位开始
            
            num *= sign;
            
            while (num > 0) {
                res += (num % 7) * digit;
                digit *= 10;
                num /= 7;
            }
            return String.valueOf(res * sign);
        }
    }
    

  • 2

    @endaye Nice Chinese characters :)


  • 0
    S

    even less verbose solution:

    public String convertToBase7(int num) {
        if (num == 0) return "0";
        else {
            StringBuilder result = new StringBuilder();
            for (int abs = Math.abs(num); abs > 0; abs /= 7) 
                result.append(abs % 7);
            if (num < 0)
                result.append("-");
            return result.reverse().toString();
        }
    }
    

  • 0
    G

    @shawngao by the way,why did you use sb.append("-");,I think it's unnecessary if you don't use Math.abs(num % 7),but maybe I'wrong as I notice StringBuider you used at last, :],so this is my code not using StringBuilder:

    public class Solution {
        public String convertToBase7(int num) {
            int qu=num,re=0;
            int fin=0,i=0;
            while(qu!=0){
                re=qu%7;
                qu/=7;
                fin+=re*Math.pow(10,i);
                i++;
            }
            return fin+"";
        }
    }
    

Log in to reply
 

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