4-lines Self-explained Java Solution #Java


  • 1

    The code is self-explained.

        public String reverseWords(String s) {
            StringBuilder sb = new StringBuilder();
            String[] words = s.trim().split(" +");
            for(int i=words.length-1; i>=0; --i) sb.append(words[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.