A strange phenomenon, the result of the lettcode platform isn't the same as my computer


  • 0
    M

    There is one test data which produces this phenomenon
    [1,2] [3,4]
    I test this data on my computer, and the answer is 2.5 while the answer is 1.0 on the leetcode platform. My compiler is GNU GCC.
    So my code can't be accepted.I don't know why!Here is my code:

    #include<stdio.h>
    #include <string.h>
    #define INT_MAX 0x7fffffff
    
    int min(int a, int b)
    {
      return a < b ? a : b;
    }
    int findKthless(int nums1[], int s1th, int nums1Size, int nums2[], int s2th, int nums2Size, int k)
    {
    
      if(k == 1)
      {
          return min(nums1[s1th], nums2[s2th]);
      }
      if(s1th >= nums1Size)
      {
          return nums2[s2th+k-1];
      }
      if(s2th >= nums2Size)
      {
          return nums1[s1th+k-1];
      }
      int s1_key, s2_key;
      if(s1th+k/2-1 >= nums1Size)
      {
          s1_key = INT_MAX;
      }
      else
      {
          s1_key = nums1[s1th+k/2-1];
      }
      if(s2th+k/2-1 >= nums2Size)
      {
          s2_key = INT_MAX;
      }
      else
      {
          s2_key = nums2[s2th+k/2-1];
      }
      if(s1_key > s2_key)
      {
          return findKthless(nums1, s1th, nums1Size, nums2, s2th+k/2, nums2Size, k-k/2);
      }
      else
      {
          return findKthless(nums1, s1th+k/2, nums1Size, nums2, s2th, nums2Size, k-k/2);
      }
    
    }
    double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size)
    {
      int size = nums1Size + nums2Size;
      if(size & 1)
      {
          return (double)findKthless(nums1, 0, nums1Size, nums2, 0, nums2Size, (size+1)/2);
      }
      else
      {
          return (double)(findKthless(nums1, 0, nums1Size, nums2, 0, nums2Size, size/2)
                        + findKthless(nums1, 0, nums1Size, nums2, 0, nums2Size, size/2+1)) / 2;
      }
    }
    int main()
    {
      int a[2] = {1, 2};
      int b[2] ={3, 4};
      printf("%lf\n", findMedianSortedArrays(a, 2, b, 2));
    }
    
    

Log in to reply
 

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