leetcode|leetcode_4

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)).
Example 1:

nums1 = [1, 3] nums2 = [2]The median is 2.0

Example 2:

nums1 = [1, 2] nums2 = [3, 4]The median is (2 + 3)/2 = 2.5



【leetcode|leetcode_4】一看到这个题目,就想到了合并归序,就是两部分相加的部分,找到中间两位数即可返回
class Solution { public: double findMedianSortedArrays(vector& nums1, vector& nums2) { int i = 0,j=0,k=0; int m = nums1.size(); int n = nums2.size(); int num[n+m]; while(i < m && j < n) { if(nums1[i]<=nums2[j]) num[k++]=nums1[i++]; else num[k++]=nums2[j++]; if(k==(n+m)/2+1) return (double(num[(m+n)/2]+num[(m+n-1)/2])/2.0); } while (i < m) { num[k++]=nums1[i++]; if(k==(n+m)/2+1) return (double(num[(m+n)/2]+num[(m+n-1)/2])/2.0); } while (j < n) { num[k++]=nums2[j++]; if(k==(n+m)/2+1) return (double(num[(m+n)/2]+num[(m+n-1)/2])/2.0); } } };


    推荐阅读