My Solution of Java


  • 0
    C
    public static String reverseWords(String s) {
        String[] res=s.trim().split(" ");
        StringBuilder builder=new StringBuilder();
        for(int i=res.length-1;i>=0;i--){
        	String temp=res[i];
        	if(!temp.trim().equals("")){
        		builder.append(temp+" ");
        	}
        }
        if(builder.length()!=0){
        	builder.deleteCharAt(builder.length()-1);
        }
        return builder.toString();
    }

Log in to reply
 

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