Java 10ms solution in place


  • 0
    Q
     public String reverseWords(String s) {
    		char[] chars = s.toCharArray();
    		int start = 0;
    		while(start<chars.length&&chars[start]==' ') start++;
    		while(start < chars.length){
    			int end = start;
    			while(end != chars.length-1 && chars[end+1]!=' ') end++;
    			
    			int index = end+2;
    			while(start<end){
    				char temp = chars[start];
    				chars[start] = chars[end];
    				chars[end] = temp;
    				end--;
    				start++;
    			}
    			while(index<chars.length&&chars[index]==' ') index++;
    			start = index;
    		}
    		return new String(chars);
    	        
    	    }
    

Log in to reply
 

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