yet another java solution


  • 0
    G
    public class Solution {
        public String reverseWords(String s) {
            StringBuilder sb = new StringBuilder(s);
            String[] as = sb.reverse().toString().split(" ");        
            int left=0,right=as.length-1;
            while(left<right){
                String temp = as[left];
                as[left] = as[right];
                as[right] = temp;
                left++;
                right--;
            }
            StringBuilder res = new StringBuilder();
            for(String str:as){
                res.append(str+" ");
            }
            return res.toString().trim();
        }
    }
    

  • 0
    L

    @gavin1818
    this solution is very good.
    i have learned a lot from you.


Log in to reply
 

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