My JAVA Solution with StringBuilder


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