题目:leetcode
class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { if (nums1.empty() && nums2.empty()) throw exception(); if (nums1.size()>nums2.size()) return findMedianSortedArrays(nums2, nums1); int lens = nums1.size(), lenl = nums2.size(); if (lens == lenl) { double r1 = MedianEqualdArrays(nums1, 0, lens - 1, nums2, 0, lenl - 1); double r2 = MedianEqualdArrays_down(nums1, 0, lens - 1, nums2, 0, lenl - 1); return (r1 + r2) / 2; } else if ((lens + lenl) & 1) return find_Kth_number(nums1, nums2, (lens + lenl) / 2 + 1); else { int index1 = (lens + lenl) / 2, index2 = index1 + 1; double r1 = find_Kth_number(nums1, nums2, index1), r2 = find_Kth_number(nums1, nums2, index2); return (r1+r2)/2; } } double find_Kth_number(vector<int>& nums1, vector<int>& nums2, int index) { if (nums1.empty()) { return nums2[index - 1]; } if (nums2.empty()) { return nums1[index - 1]; } int lens = nums1.size(), lenl = nums2.size(); if (index <= lens) { return MedianEqualdArrays(nums1, 0, index - 1, nums2, 0, index - 1); } else if (index>lenl) { int offset = lens + lenl - index, index1 = lens - offset, index2 = lenl - offset; return MedianEqualdArrays(nums1, index1 - 1, lens - 1, nums2, index2 - 1, lenl - 1); } else// (lens,lensl] { int nums2_begin = index - lens - 1, nums_end = index - 1; if (nums2[nums2_begin] >= nums1.back()) return nums2[nums2_begin]; else { return MedianEqualdArrays(nums1, 0, lens - 1, nums2, nums2_begin + 1, nums_end); } } } //返回的是比较小的那个中位数 double MedianEqualdArrays(vector<int>& nums1, int begin1, int end1, vector<int>& nums2, int begin2, int end2) { int len = end1 - begin1 + 1; if (len == 1) { return min(nums1[begin1], nums2[begin2]); } else { int move_step = len / 2; if ((len & 1) == 0) { --move_step; } int mid1 = begin1 + move_step, mid2 = begin2 + move_step; if (nums1[mid1] == nums2[mid2]) return nums1[mid1]; else if (nums1[mid1]<nums2[mid2]) { if (len & 1) return MedianEqualdArrays(nums1, mid1, end1, nums2, begin2, mid2); else return MedianEqualdArrays(nums1, mid1 + 1, end1, nums2, begin2, mid2); } else//nums1[mid1]>nums2[mid2] { if (len & 1) return MedianEqualdArrays(nums1, begin1, mid1, nums2, mid2, end2); else return MedianEqualdArrays(nums1, begin1, mid1, nums2, mid2 + 1, end2); } } } //返回的是比较大的那个中位数 double MedianEqualdArrays_down(vector<int>& nums1, int begin1, int end1, vector<int>& nums2, int begin2, int end2) { int len = end1 - begin1 + 1; if (len == 1) { return max(nums1[begin1], nums2[begin2]); } else { int move_step = len / 2; int mid1 = begin1 + move_step, mid2 = begin2 + move_step; if (nums1[mid1] == nums2[mid2]) return nums1[mid1]; else if (nums1[mid1]<nums2[mid2]) { if (len & 1) return MedianEqualdArrays_down(nums1, mid1, end1, nums2, begin2, mid2); else return MedianEqualdArrays_down(nums1, mid1, end1, nums2, begin2, mid2 - 1); } else//nums1[mid1]>nums2[mid2] { if (len & 1) return MedianEqualdArrays_down(nums1, begin1, mid1, nums2, mid2, end2); else return MedianEqualdArrays_down(nums1, begin1, mid1 - 1, nums2, mid2, end2); } } } };
原文地址:http://blog.csdn.net/bupt8846/article/details/45559877