Clean code in O(n)


  • 0
    M
    public String reverseWords(String s) {
        int len=s.length();
        if(s==null||len==0)
        return "";
        
        s=s.trim();
        String[] array=s.split(" ");
        StringBuffer sb=new StringBuffer();
        
        for(int i=array.length-1;i>=0;i--){
            if(!array[i].equals("")){
            sb.append(array[i]);
            sb.append(" ");
            }
        }
        String res=sb.toString();
        res=res.trim();
        return res;
        
    }

Log in to reply
 

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