Java Solution [7 lines] - Very Easy to Understand


  • 0
    public class Solution {
        public String reverseWords(String s) {
            String[] ss = s.split(" ");
            StringBuilder sb = new StringBuilder();
            for (String w : ss) {
                sb.insert(0, w.toCharArray());
                sb.insert(0, " ");
            }
            return sb.reverse().toString().trim();
        }
    }
    

Log in to reply
 

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