Java Simple Solution


  • 0
    M
    public class Solution {
        public String reverseWords(String s) {
            String[] parts = s.split(" ");
            StringBuilder res = new StringBuilder();
            for (String part :  parts) {
                StringBuilder sb = new StringBuilder();
                res.append(sb.append(part).reverse().toString());
                res.append(" ");
            }
            res.setLength(res.length()-1);
            return res.toString();
        }
    }
    

Log in to reply
 

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