My accepted Java Solution. lazy but Straightforward


  • 1
    J
    public class Solution {
        public String reverseWords(String s) {
    		if(s==null||s.length()==0)
    			return "";
    		s = s.trim();
    		StringBuilder sb = new StringBuilder(s);
    		sb.reverse();
    		String[] store = sb.toString().split(" +");
    		StringBuilder temp;
    		StringBuilder result = new StringBuilder();
            for (String string : store) {
            	 temp = new StringBuilder(string);
            	 temp.reverse();
            	 result.append(temp.toString()+" ");
    		}
            return result.toString().trim();
    	}
    }

Log in to reply
 

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