My Java Solution


  • 0
    I
    public String reverseWords(String s) {
    	StringBuilder sb = new StringBuilder();
    	String[] words = s.split("[ ]");
    	
    	for (int i = words.length - 1; i >= 0; i--) {
    		if (words[i].equals("")) {continue;}
    		sb.append(words[i] + " ");
    	}
    	if (sb.length() > 0) {
    		if (sb.substring(sb.length() - 1, sb.length()).equals(" ")) {
    			sb.delete(sb.length() - 1, sb.length());
    		}
    	}
    	return sb.toString();
    }

Log in to reply
 

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