There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
一開始直觀 就是先檢查有沒有一個數列完全比另外一個大,但是這樣做會讓CODE變長LOL
接著在去慢慢比較找中位數
這是比較笨的寫法,看到有人用更神的
還要再學習阿~
int checkoverlap(int* nums1, int nums1Size, int* nums2, int nums2Size){ int flag=0; if(nums1[ nums1Size-1 ] < nums2[0] ){//nums1 is less than 2 return 1; }else if(nums2[ nums2Size-1 ] < nums1[0]){//nums2 is less than 1 return 2; }else {// overlap return 3; } } double case1(int* nums1, int nums1Size, int* nums2, int nums2Size){ int medin=(nums1Size+nums2Size)/2 +1 ;//because the devide won't round up 5/2=2,6/2=3 if( nums1Size < medin ){//inside the nums2 if( (nums1Size+nums2Size)%2==0 ){ return ( (medin-nums1Size ==1) ? (nums1[nums1Size-1]+nums2[0])/2.0 : (nums2[medin - nums1Size-1]+nums2[medin - nums1Size-2] )/2.0 ); //medin is in the first nums2: not in the first }else { return nums2[medin - nums1Size -1 ]; } }else if( nums1Size > medin ){//inside the nums1 return ( ((nums1Size+nums2Size)%2==0 ) ? (nums1[medin -1]+nums1[medin -2] )/2.0 : nums1[medin -1 ] ); //even: odd }else if( nums1Size == medin ){//medin is in the last number of nums1 return ( (nums1Size+nums2Size)%2==0 ? (nums1[nums1Size-1] + nums1[nums1Size-2] )/2.0 : nums1[nums1Size-1] ); //even, last2 number in nums1: last number in nums1 } } double case2(int* nums1, int nums1Size, int* nums2, int nums2Size){ int medin=(nums1Size+nums2Size)/2 +1 ;//because the devide won't round up 5/2=2,6/2=3 if( nums2Size < medin ){//inside the nums1 if( (nums1Size+nums2Size)%2==0 ){ return ( (medin-nums2Size ==1) ? (nums2[nums2Size-1]+nums1[0])/2.0 : (nums1[medin - nums2Size-1]+nums1[medin - nums2Size-2] )/2.0 ); //medin is in the first nums2: not in the first }else { return nums1[medin - nums2Size -1 ]; } }else if( nums2Size > medin ){//inside the nums2 return ( ((nums1Size+nums2Size)%2==0 ) ? (nums2[medin -1]+nums2[medin -2] )/2.0 : nums2[medin -1 ] ); //even: odd }else if( nums2Size == medin ){//medin is in the last number of nums1 return ( (nums1Size+nums2Size)%2==0 ? (nums2[nums2Size-1] + nums2[nums2Size-2] )/2.0 : nums2[nums2Size-1] ); //even, last2 number in nums1: last number in nums1 } } double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) { if(nums1Size==0){ if(nums2Size ==1)return nums2[0]; return ( nums2Size%2==0 ? (nums2[nums2Size/2]+nums2[(nums2Size/2) - 1])/2.0 : nums2[nums2Size/2] );//even:odd } if(nums2Size==0){ if(nums1Size ==1)return nums1[0]; return ( nums1Size%2==0 ? (nums1[nums1Size/2]+nums1[nums1Size/2 - 1])/2.0 : nums1[nums1Size/2] ); } //find the overlap int medin=(nums1Size+nums2Size)/2 +1 ;//because the devide won't round up 5/2=2,6/2=3 int sum= nums1Size + nums2Size; int nums1P,nums2P,remin=0; int previous=0,current=0; switch ( checkoverlap( nums1, nums1Size, nums2, nums2Size) ){ case 1://nums1 < nums2 return case1( nums1, nums1Size, nums2, nums2Size); break; case 2://nums2 < nums1 return case2( nums1, nums1Size, nums2, nums2Size); break; case 3://overlap nums1 nums2 nums1P=nums2P=1; //count how many number need to process remin=medin; while ( nums2P<=nums2Size || nums1P<=nums1Size ){ if( nums1P<= nums1Size && nums1[nums1P-1] <= nums2[nums2P -1] ){ previous = current; current = nums1[nums1P-1]; nums1P++; remin--; //if( nums1P > nums1Size)remin--; }else if( nums2P<=nums2Size && nums2[nums2P -1 ] < nums1[nums1P - 1] ){ previous = current ; current = nums2[nums2P-1]; nums2P++; remin--; //if( nums2P > nums2Size)remin--; }else if( nums2P > nums2Size ){ previous = current; current = nums1[nums1P-1]; nums1P++; remin--; }else if( nums1P > nums1Size){ previous = current ; current = nums2[nums2P-1]; nums2P++; remin--; } if(remin<=0 ){ return ( ((nums1Size+nums2Size)%2==0 ) ? (previous+ current)/2.0 :current); break; } } break; } }
沒有留言:
張貼留言