Share My diverse java solutions


  • 0
    M
    1. (3ms)
        public String reverseWords(String str) {
        String[] s = **str.trim().split(" ");**
        StringBuffer sb = new StringBuffer();
        if(s.length<1) return "";
        
        sb.append(s[s.length-1]);
        
        for(int i=s.length-2;i>=0;i--){
            if(s[i].length() <1) continue;
            sb.append(' ').append(s[i]);    
        }
        return sb.toString();
        }
    
    1. (4ms)

       public String reverseWords(String str) {
       String[] s = **str.split(" ");**
       StringBuffer sb = new StringBuffer();
       if(s.length<1) return "";
       
       sb.append(s[s.length-1]);
       
       for(int i=s.length-2;i>=0;i--){
           if(s[i].length() <1) continue;
           sb.append(' ').append(s[i]);    
       }
       return sb.toString();
       }
      
    2. (11ms)

        public String reverseWords(String str) {
        String[] s = **str.split(" +");**
        StringBuffer sb = new StringBuffer();
        if(s.length<1) return "";
        
        sb.append(s[s.length-1]);
        
        for(int i=s.length-2;i>=0;i--){
            if(s[i].length() <1) continue;
            sb.append(' ').append(s[i]);    
        }
        return sb.toString();
        }

Log in to reply
 

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