This is my simple Solution in Java with O(n)


  • 0
    R
    public class Solution {
    public String reverseWords(String s) {
        String sx = new String();
        if(s == null)
          return null;
          
          char c[] = s.trim().toCharArray();
          
          for(int i =0; i< c.length ;){
              StringBuffer sb = new StringBuffer();
              while(i< c.length && c[i] != ' '  ){
                  sb.append(c[i]);
                  i++;
              }
              while(i< c.length && c[i] == ' ' ){
                  i++;
              }
              sx = " "+ sb.toString()+ sx;
          }
     
         return sx.trim();
    }
    

    }


Log in to reply
 

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