Merger two sorted arrays not working? can anyone help


  • 0
    R

    void merge(int* nums1, int m, int* nums2, int n) {
    int i=0,j=0,k=0;
    while(i<m&&j<n)
    {
    if(nums1[i]<nums2[j])
    {
    nums1[k]=nums1[i];
    i++;
    k++;
    }
    else if (nums1[i]>nums2[j])
    {
    nums1[k]=nums2[j];
    k++;
    j++;
    }
    else
    {nums1[k]=nums1[i];
    k++;i++;j++;}
    }

    while(i!=m)
    {  
        nums1[k]=nums1[i];
          k++,i++;
          
        
    }
    while(j!=n)
    {
        nums1[k]=nums2[j];
          k++,j++;
          
    }
    
    return nums1;
    

    }


Log in to reply
 

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