Very simple Java Solution with self explanatory and Clean code


  • 1
    R
     public String reverseWords(String s) {
            char[] input = s.toCharArray();
    
            int start = 0;
            int end = 0;
    
            int i=0;
    
            while(i < input.length){
    
                while(i < input.length && Character.isWhitespace(input[i])){
                    i++;
                }
                start = i;
    
                while(i < input.length && !Character.isWhitespace(input[i])){
                    i++;
                }
                end = i-1;
                reverse(input,start,end);
            }
    
            return new String(input);
        }
    
        public void reverse(char[] input, int start, int end){
            while(start <= end){
                char temp = input[start];
                input[start] = input[end];
                input[end] = temp;
                start++;
                end--;
            }
        }
    
    

Log in to reply
 

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