Simple Accepted Java Soln


  • 0
    R
    public String reverseWords(String s) {
            String[] words = s.split(" ");
            StringBuilder sb = new StringBuilder(s.length());
            for(int k=0; k<words.length; k++){
                char[] c = words[k].toCharArray();
                int i=0, j=c.length-1;
                while(i < j){
                    char tmp = c[i]; c[i++] = c[j]; c[j--] = tmp;
                }
                sb.append(c);
                if(k != words.length-1) sb.append(" "); // to not put space after last word
            }
            return sb.toString();
        }
    

Log in to reply
 

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