Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size){
- int p1=0;
- int p2=0;
- int i=0;
- int j=0;
- int k=(nums1Size+nums2Size)%2;
- int e11=-1;
- int e12=-1;
- int e21=-1;
- int e22=-1;
- //printf("k=%d\n",k);
- while(i<nums1Size){
- //printf("hi\n");
- while(j<nums2Size&&nums1[i]>=nums2[j])
- j++;
- if((i+j==(nums1Size+nums2Size)/2)&&k==1){
- p1=i;return nums1[i];
- }
- if(i+j==(nums1Size+nums2Size)/2-1&&k==0){
- e11=i;
- //printf("1record%d\n",i);
- }
- if((i+j==(nums1Size+nums2Size)/2&&k==0)){
- e12=i;
- //printf("1record%d\n",i);
- }
- i++;
- }
- i=0;
- j=0;
- while(i<nums1Size){
- //printf("hi\n");
- while(j<nums2Size&&nums1[i]>nums2[j])
- j++;
- if((i+j==(nums1Size+nums2Size)/2)&&k==1){
- p1=i;return nums1[i];
- }
- if(i+j==(nums1Size+nums2Size)/2-1&&k==0){
- e11=i;
- //printf("1record%d\n",i);
- }
- if((i+j==(nums1Size+nums2Size)/2&&k==0)){
- e12=i;
- //printf("1record%d\n",i);
- }
- i++;
- }
- i=0;
- j=0;
- while(j<nums2Size){
- while(i<nums1Size&&nums2[j]>=nums1[i])
- i++;
- if(i+j==(nums1Size+nums2Size)/2&&k==1){
- p2=j;//printf("2way\n");
- return nums2[j];
- }
- if(i+j==(nums1Size+nums2Size)/2-1&&k==0){
- e21=j;
- //printf("2record%d\n",j);
- }
- if((i+j==(nums1Size+nums2Size)/2&&k==0)){
- e22=j;
- //printf("2record%d\n",j);
- }
- j++;
- }
- i=0;
- j=0;
- while(j<nums2Size){
- while(i<nums1Size&&nums2[j]>nums1[i])
- i++;
- if(i+j==(nums1Size+nums2Size)/2&&k==1){
- p2=j;//printf("2way\n");
- return nums2[j];
- }
- if(i+j==(nums1Size+nums2Size)/2-1&&k==0){
- e21=j;
- //printf("2record%d\n",j);
- }
- if((i+j==(nums1Size+nums2Size)/2&&k==0)){
- e22=j;
- //printf("2record%d\n",j);
- }
- j++;
- }
- //printf("e11:%d,e12:%d,e21:%d,e22:%d",e11,e12,e21,e22);
- if(e21*e22>=0&&e21+e22>=0){
- //printf("**1\n");
- return (nums2[e21]+nums2[e22])/2.0;
- }
- if(e11*e12>=0&&e11+e12>=0){
- //printf("**2\n");
- return (nums1[e11]+nums1[e12])/2.0;
- }
- if(e11*e21>=0&&e11+e21>=0){
- //printf("**3\n");
- return (nums1[e11]+nums2[e21])/2.0;
- }
- if(e11*e22>=0&&e11+e22>=0){
- //printf("**4\n");
- return (nums1[e11]+nums2[e22])/2.0;
- }
- if(e12*e21>=0&&e12+e21>=0){
- //printf("**5\n");
- return (nums1[e12]+nums2[e21])/2.0;
- }
- if(e12*e22>=0&&e12+e22>=0){
- //printf("**6\n");
- return (nums1[e12]+nums2[e22])/2.0;
- }
- printf("return 0");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement