Java solution, 11 lines


  • 0
    L
        public void reverseWords(char[] s) {
            reverse(s, 0, s.length - 1);
            for (int i = 0, j = 0; i < s.length ; ){
                while (j < s.length && s[j] != ' ') j++;
                reverse(s,i,j++ - 1);
                i = j;
            }
        }
        
        public void reverse(char[] s, int start, int end){
            while (start < end){
                char temp = s[start];
                s[start++] = s[end];
                s[end--] = temp;
            }
        }
    

Log in to reply
 

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