(C++)Time Limit Exceeded, what's wrong with my code while the input is [1],[]


  • 0
    H

    I'm really a green hand and don't know how to test the input of [1],[]. My code is as follows:

    class Solution
    {
    public:
    	void merge(int A[], int m, int B[], int n) 
    	{
    		int currentindex = m-1;
    		int index_a=m-n-1;
    		int index_b=n-1;
    
    		while(index_a!=-1 || index_b!=-1)
    		{				
    			if(index_a!=-1&&index_b!=-1)
    			{
    				if(*(A+index_a)>*(B+index_b))
    				{
    					*(A+currentindex) = *(A+index_a);
    					index_a--;
    				}
    				else
    				{
    					*(A+currentindex) = *(B+index_b);
    					index_b--;
    				}
    			}
    			else if(index_b!=-1 && !(index_a!=-1))
    			{
    				*(A+currentindex) = *(B+index_b);
    				index_b--;
    			}
    			currentindex--;
    		}	
    	}
    };

Log in to reply
 

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