C AC,short and clean, any improvement?


  • -1
    A
    double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
        int odd = (nums1Size + nums2Size) % 2;
        int mid = (nums1Size + nums2Size) / 2;
        double ans;
        int *s1 = nums1;
        int *s2 = nums2;
        int cur;
        int i;
        int next;
    
         for(i = 0; i < mid + odd; i++){
             if(((s2 - nums2) < nums2Size) && ((s1 - nums1) < nums1Size)){
                 if(*s1 > *s2) {cur = *s2++;}
                 else {cur = *s1++;}
             }else{
                 if((s2 - nums2) < nums2Size) {cur = *s2++;}
                 else {cur = *s1++;}
             }
         }
         
        if(odd) ans = cur;
        else{
            if(((s2 - nums2) < nums2Size) && ((s1 - nums1) < nums1Size)){
                if(*s1 > *s2) next = *s2;
                else next = *s1;
            }
            else{
                if((s2 - nums2) < nums2Size) next = *s2;
                else next = *s1;
            }
            ans = ((double)cur+next)/2;
        }
     
        return ans;    
    }
    

    i have been simplified it from

    double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
        int odd = (nums1Size + nums2Size) % 2;
        int mid = (nums1Size + nums2Size) / 2;
        double ans;
        int *s1 = nums1;
        int *s2 = nums2;
        int cur;
        int i;
        int next;
        if(odd){
            for(i = 0; i <= mid; i++){
                if(((s2 - nums2) < nums2Size) && ((s1 - nums1) < nums1Size)){
                    if(*s1 > *s2) cur = *s2++;
                    else cur = *s1++;
                }else{
                    if((s2 - nums2) < nums2Size) cur = *s2++;
                    else cur = *s1++;
                }
            }
            ans = cur;
        }
        else{
            for(i = 0; i < mid; i++){
                if(((s2 - nums2) < nums2Size) && ((s1 - nums1) < nums1Size)){
                    if(*s1 > *s2) cur = *s2++;
                    else cur = *s1++;
                }else{
                    if((s2 - nums2) < nums2Size) cur = *s2++;
                    else cur = *s1++;
                }
            }
            
            if(((s2 - nums2) < nums2Size) && ((s1 - nums1) < nums1Size)){
                if(*s1 > *s2) next = *s2;
                else next = *s1;
            }
            else{
                if((s2 - nums2) < nums2Size) next = *s2;
                else next = *s1;
            }
            ans = ((double)cur+next)/2;
        }
     
        return ans;    
    }
    

Log in to reply
 

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