Beats around 42% of other solutions in JAVA


  • 0
    R
        public String reverseWords(String s) {
    
            StringBuffer sb = new StringBuffer();
            StringBuffer result = new StringBuffer();
            char[] c = s.toCharArray();
            for(char cc : c) {
                if(cc==' ') {
                    result.append(sb.reverse().toString());
                    result.append(" ");
                    sb = new StringBuffer();
                } else {
                    sb.append(cc);
                }
            }
            result.append(sb.reverse().toString());
            
            return result.toString();
            
        }
    

Log in to reply
 

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