Accepted cpp code


  • 0
    G
    class Solution {
    public:
        void reverseWords(string &s) 
    	{
    		int size=s.size();
    		if(size<=1) return;
    
    		int p=0;
    		int q=size-1;
    
    		while(p<q)
    		{
    			char tmp=s[p];
    			s[p]=s[q];
    			s[q]=tmp;
    
    			p++;
    			q--;
    		}
    
    		q=p=0;
    		while(q<size)
    		{
    			while(q<size && s[q]!=' ')
    			{
    				q++;
    			}
    
    			int p1=p;
    			int q1=q-1;
    			while(p1<q1)
    			{
    				char tmp=s[p1];
    				s[p1]=s[q1];
    				s[q1]=tmp;
    
    				p1++;
    				q1--;
    			}
    
    			p=q+1;
    			q=p;
    		}
        }
    };

Log in to reply
 

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