My solution in Java. Accepted 712ms.


  • 0
    A
        public String reverseWords(String s) {
        		StringBuilder stringBuilder = new StringBuilder();
    	int count = 0;
    	s = s.trim();
    	
    	for(int i=s.length()-1; i>=0; i--){
    		count++;
    		if((s.charAt(i) == ' ') || (i == 0)){				
    			int tmp = i;
    			if(i==0) {
    				stringBuilder.append(' ');
    			}
    			for(int j = 0; j < count; j++) {
    				stringBuilder.append(s.charAt(tmp+j));
    			}
    		stringBuilder = new StringBuilder(stringBuilder.toString().trim());
    		count = 0;
    		}	
    		else
    			continue;
    	}
    	return stringBuilder.toString().trim();
    }

Log in to reply
 

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