Java solution


  • 0
    Z
    public static String reverseWords(String s) {
    		int i=0,start=0;
    		char[] input = s.toCharArray();
    		while(i<input.length){
    			if(input[i] == ' '){
    				reverse(input, start, i-1);
    				start=i+1;
    			}
    			i++;
    		}
    		reverse(input, start, input.length-1);
    		return new String(input);
        }
    
    private static 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.