Simple java solution


  • 0
    S

    import java.util.*;
    public class Solution {
    public String reverseWords(String s) {
    int len=s.length();
    if (len==1 && s=="")
    return "";
    else if(len==1)
    return s.trim();
    else
    {

    	StringBuilder sb=new StringBuilder();
    	StringBuilder sb1=new StringBuilder();
    	StringTokenizer st = new StringTokenizer(s);
        while (st.hasMoreTokens()) {
        	 sb.append(st.nextToken()+" ");
        }
        s=sb.toString();
       
        String[] result = s.split("\\s");
        len=result.length;
      		for(int i=len-1;i>=0;i--)
      		{
      		    if(result[i]!=" ")
      			 sb1.append(result[i]+" ");
      			
      		}
    	return sb1.toString().trim();
      	}
    }
    

    }


Log in to reply
 

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