Why can't the input[[1,2,3], [4,5,6]] pass but it is right tested on my vs2012 with the input?


  • 0
    Y
    class Solution {
    public:
        double findMedianSortedArrays(int A[], int m, int B[], int n) {
            double A_mid,B_mid;
    	    if(m==0) {
    	        if(n%2==1) return double(B[n/2]);
    	        else return (B[(n/2)-1]+B[n/2])/2.0;
    	    }
    	    if(n==0){
    	        if(m%2==1) return double(A[m/2]);
    	        else return (A[(m/2)-1]+A[m/2])/2.0;
    	    }
    	    if(m%2==1) A_mid=double(A[m/2]);
    	    else A_mid=(A[(m/2)-1]+A[m/2])/2.0;
    	    if(n%2==1) B_mid=double(B[n/2]);
    	    else B_mid=(B[(n/2)-1]+B[n/2])/2.0;
    
    	    if(A_mid==B_mid) return A_mid;
    	    else if(m<=2){
    		    int *temp=new int[m+n];
    	        int i=0;
    		    int j=0;
    		    int e=0;
    		    while (i<n&&j<m){
    			    if(A[j]<B[i]) temp[e++]=A[j++];
    			    else temp[e++]=B[i++];
    		    }
    		    while (i<n){
    			    temp[e++]=B[i++];
    		    }
    		    while (j<m){
    			    temp[e++]=A[j++];
    		    }
    		    double res=0.0;
    		    if ((m+n)%2==1) {
    		    	res=double(temp[(m+n)/2]);
    		    	delete []temp;
    	    	}else {
    		    	res=(temp[((m+n)/2)-1]+temp[(m+n)/2])/2.0;
    		    	delete []temp;
    	    	}
    	    	return res;
        	}else if(n<=2){
    	    	int *temp=new int[m+n];
    	        int i=0;
    		    int j=0;
    		    int e=0;
    		    while (i<n&&j<m){
    			    if(B[i]<A[j]) temp[e++]=B[i++];
    			    else temp[e++]=A[j++];
    		    }
    		    while (i<n){
    			    temp[e++]=B[i++];
    		    }
    		    while (j<m){
    			    temp[e++]=A[j++];
    		    }
    	    	double res=0.0;
    		    if ((m+n)%2==1) {
    		    	res=double(temp[(m+n)/2]);
    			    delete []temp;
    		    }else {
    			    res=(temp[((m+n)/2)-1]+temp[(m+n)/2])/2.0;
    			    delete []temp;
    		    }
    	    	return res;
    	    }
    	
    	    else if(A_mid > B_mid){
    		    if (m>=n){
    		    	int *temp_A=new int[m-n/2];
    		    	int *temp_B=new int[n-n/2];
    			    int ind_A=0;
    			    int ind_B=0;
    			    for (int k=0;k<(m-n/2);k++) temp_A[ind_A++]=A[k];
    			    for (int k=n/2;k<n;k++)     temp_B[ind_B++]=B[k];
    			    findMedianSortedArrays(temp_A,m-n/2,temp_B,n-n/2);
    		
    		    }else{
    			    int *temp_A=new int[m-m/2];
    			    int *temp_B=new int[n-m/2];
    			    int ind_A=0;
    		    	int ind_B=0;
    			    for (int k=0;k<(m-m/2);k++) temp_A[ind_A++]=A[k];
    			    for (int k=m/2;k<n;k++)     temp_B[ind_B++]=B[k];
    			    findMedianSortedArrays(temp_A,m-m/2,temp_B,n-m/2);
    		    }
    	    }
    	    else{
    		    if (m>=n){
    			    int *temp_A=new int[m-n/2];
    			    int *temp_B=new int[n-n/2];
    			    int ind_A=0;
    			    int ind_B=0;
    			    for (int k=n/2;k<m;k++)     temp_A[ind_A++]=A[k];
    			    for (int k=0;k<(n-n/2);k++) temp_B[ind_B++]=B[k];
    		        findMedianSortedArrays(temp_A,m-n/2,temp_B,n-n/2);
    		
    		    }else{
    			    int *temp_A=new int[m-m/2];
    			    int *temp_B=new int[n-m/2];
    			    int ind_A=0;
    			    int ind_B=0;
    		    	for (int k=m/2;k<m;k++) temp_A[ind_A++];
    		    	for (int k=0;k<(n-m/2);k++) temp_B[ind_B++]=B[k];
    		    	findMedianSortedArrays(temp_A,m-m/2,temp_B,n-m/2);
    		    }
    	
    	    }
    
        }
    };

Log in to reply
 

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