3ms C


  • 0
    J

    /**

    • Return an array of size *returnSize.
    • Note: The returned array must be malloced, assume caller calls free().
      /
      void myQsort(int a[], int low, int high){
      int first = low;
      int last = high;
      int key = a[low];
      if(low >= high) return;
      while(first < last){
      while(first < last && a[last] >= key) last--;
      a[first] = a[last];
      while(first < last && a[first] <= key) first++;
      a[last] = a[first];
      }
      a[first] = key;
      myQsort(a, low, first);
      myQsort(a, first+1, high);
      }
      int
      intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
      int res = NULL;
      int i,j=0,k=0;
      myQsort(nums1, 0, nums1Size-1);
      myQsort(nums2, 0, nums2Size-1);
      res = malloc(sizeof(int)
      (nums1Size+nums2Size));
      for(i = 0; i < nums1Size; i++){
      if(j == nums2Size) break;
      if(nums1[i] > nums2[j]){
      i--;
      j++;
      }
      else if(nums1[i] == nums2[j]){
      if(k==0) res[k++] = nums1[i];
      else if(res[k-1] != nums1[i]) res[k++] = nums1[i];
      j++;
      }
      else{
      //do nothing;
      }
      }
      *returnSize = k;
      return res;
      }

Log in to reply
 

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