Simple Java AC Solution


  • 0
    J
    	public String convertToBase7(int num) {
    		if (num == 0) {
    			return "0";
    		}
    		int x = Math.abs(num);
    		StringBuilder sb = new StringBuilder();
    		while (x != 0) {
    			sb.append(x % 7);
    			x /= 7;
    		}
    		if (num < 0) {
    			sb.append("-");
    		}
    		return sb.reverse().toString();
    
    	}
    

Log in to reply
 

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