Using Regex 7 linese code


  • 0
    Y
    public class Solution {
    public String reverseWords(String s) {
        String[] strs=s.trim().replaceAll("[ ]+", " ").split(" ");
        StringBuilder sb=new StringBuilder();
        for(int i=strs.length-1; i>=0; i--){
            if(i==0) sb.append(strs[i]);
            else sb.append(strs[i]).append(" ");
        }
        
        return sb.toString();
    }
    

    }


  • 0
    K
    This post is deleted!

Log in to reply
 

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