Java Solution Using StringBuffer


  • 0
    S
        public String reverseWords(String s) {
            String[] arr = s.split(" ");
            String rev = "";
            for(int i = 0; i < arr.length; i++){
                String str = arr[i];
                String rts = new StringBuffer(str).reverse().toString();
                arr[i] = rts;
            }
            for(int i = 0; i < arr.length-1; i++){
                rev += arr[i] + " ";
            }
            return rev + arr[arr.length-1];
        }
    }

Log in to reply
 

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