Java with Stack


  • 0
    2
        public String convertToBase7(int num) {
            int temp = 0;        
            Stack<Integer> s = new Stack<Integer>();
            
            if(num <6 && num >= -6){
                return Integer.toString(num);
            }
            
            if(num == 7){
                return "10";
            }
            
            if(num == -7){
                return "-10";
            }
            
            while(num!=0){
                s.push(10*temp+num%7);
                num/=7;
            }
            
            while(!s.isEmpty()){
                temp = temp*10+s.pop();
            }
            
            return Integer.toString(temp);
        }
    

Log in to reply
 

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