My solution. java


  • 0
    A
    public class Solution {
    public String reverseWords(String s) {
        String result="";
    	s=s.trim();	
    	if(s=="")
    	{
    		return s;
    	}
    	s=" "+s;
    	int end=s.length();
    	for(int i=s.length()-1;i>=0;i--)
    	{
    		if(s.charAt(i)==' ')
    		{
    			result=result+s.substring(i,end);
    			if(i!=0)
    			{
    				while(s.charAt(i-1)==' ')
    				{
    					i=i-1;
    				}
    				end=i;
    			}
    		}
    	}
    	return result.trim();
    }
    

    }


Log in to reply
 

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