What's wrong with this code?


  • 0
    S
        public String reverseWords(String s) {
            Stack stk = new Stack();
            String out ="";
            for(int i=0; i<s.length(); i++){  
                if(s.charAt(i) != ' '){
                    stk.push(s.charAt(i));
                }
                else {                
                    while(!stk.empty()){
                        out = out+stk.pop();
                    }   
                    out = out+" ";
                }            
            }   
            while(!stk.empty()){
                out = out+stk.pop();
            }          
            return out;
        }
    

    This code works fine on Eclipse. However, leetcode says this:

    0_1500748587560_2017-07-22 14_35_36-Reverse Words in a String III - LeetCode.jpg


Log in to reply
 

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