C++ easy to understand iterative solution


  • 0
        string convertToBase7(int num) {
            if(!num) return "0";
            string res="";
            bool b=false;
            if(num<0) b=true,num=-num;
            while(num>0){
                res=(char)(num%7+'0')+res;
                num/=7;
            }
            if(b) res="-"+res;
            return res;
        }
    

Log in to reply
 

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