Easiest Java solution


  • 0
    N
    public String reverseWords(String s) {
            StringBuilder sb = new StringBuilder(s).reverse();
            String[] strs= sb.toString().split("\\s+");
            sb= new StringBuilder("");
            for(int i=strs.length-1;i>=0;i--)
                sb.append(strs[i]+" ");
           
            return sb.toString().trim();
        }

Log in to reply
 

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