感觉这题比较经典,之前自己用4个指针指来指去,把自己都绕晕了,现在发现2个指针就行。找数组中位数,相当于找第k个元素,k=(nums1.size()+nums2.size())/2。
4. Median of Two Sorted Arrays
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
详细题解请见九章算法微博: http://weibo.com/3948019741/Bz1uAswtv
以下为代码