I take an advantage of java string api(s), and just a few lines

  • 0
       public String reverseWords(String s) {
            String[] strs = s.split("\\s+");
    		String reverse="";			
    		if (s.length()==0) return "";
    		if (strs.length==0) return "";
    		for (int i=strs.length-1;i>=1;i--)
    			reverse=reverse.concat(strs[i]+" ");
    		return reverse.concat(strs[0]).trim();

Log in to reply

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