Java solution


  • 0

    use StringBuilder's reverse()

    public class Solution {
        public String reverseWords(String s) {
            String[] a = s.split(" ");
            StringBuilder out =new StringBuilder();
            for(int i = 0;i< a.length;i++){
               String sb = new StringBuilder(a[i]+" ").reverse().toString();
               out.append(sb+" ");
            }
            return out.toString().trim();
        }
    }
    

Log in to reply
 

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