Java solution


  • 0
    A
    public class Solution {
        public String convertToBase7(int num) {
            StringBuffer sb=new StringBuffer();
            boolean flag=num<0? true:false;
            num=Math.abs(num);
            while(num>=7){
                sb.insert(0,num%7);
                num/=7;
            }
            if(flag==true) sb.insert(0,-num%7);
            else sb.insert(0,num%7);
            return sb.toString();
        }
    }
    

Log in to reply
 

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