leetcode_Median of Two Sorted Arrays
来源:互联网 发布:我国历年gdp数据 编辑:程序博客网 时间:2024/05/17 22:45
描述:
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)).
思路:
1.题意大概为查找两个数组 的中位数,这个不难,O(N)的时间复杂度就可以解决,但题目的要求是用 O(log (m+n))的时间复杂度来解决该问题!
2.由于时间复杂度为 O(log (m+n)),一般会用到二分法,每次取数组nums1和nums2所剩下的部分的中间值mid1和mid2,然后比较两个数组的中间值大小,较大的一个取数组较小的一部分,较小的mid数组取较大的一部分。由于每次都要多虑到一部分不符合条件的,所以刚开始要求第k=(len1+len2)/2个数的k每次都要更新。
3.重负步骤2,直至每个数组剩下的有效部分为0,则另一个数组的nums[start+k]即为中位数;另外一种可能是最后当k==0时,两个数组有效部分的开始的那个数肯定为要求的数。
哎,表述能力不行啊,自己都快被绕晕了(^-^)!
代码:
public double findMedianSortedArrays(int[] nums1, int[] nums2) { if((nums1==null||nums1.length==0)&&(nums2==null||nums2.length==0)) return 0; if(nums1==null||nums1.length==0) return getMidOfOneArray(nums2); if(nums2==null||nums2.length==0) return getMidOfOneArray(nums1);int totalLen = nums1.length + nums2.length;if ((totalLen & 0x1) == 1)return findMid(nums1, 0, nums1.length - 1, nums2, 0,nums2.length - 1, totalLen / 2);elsereturn (findMid(nums1, 0, nums1.length - 1, nums2, 0,nums2.length - 1, totalLen / 2) + findMid(nums1, 0,nums1.length - 1, nums2, 0, nums2.length - 1,totalLen / 2 - 1)) * 0.5;}public double findMid(int nums1[], int start1, int end1, int nums2[],int start2, int end2, int k) {int aLen = end1 - start1 + 1;int bLen = end2 - start2 + 1;if (aLen == 0)return nums2[start2 + k];if (bLen == 0)return nums1[start1 + k];if (k == 0)return nums1[start1] < nums2[start2] ? nums1[start1]: nums2[start2];int aMid = aLen * k / (aLen + bLen);int bMid = k - aMid - 1;aMid = aMid + start1;bMid = bMid + start2;if (nums1[aMid] > nums2[bMid]) {k = k - (bMid - start2 + 1);end1 = aMid;start2 = bMid + 1;} else {k = k - (aMid - start1 + 1);end2 = bMid;start1 = aMid + 1;}return findMid(nums1, start1, end1, nums2, start2, end2, k);}public double getMidOfOneArray(int arr[]){ if((arr.length&0x1)==0) return (arr[arr.length/2]+arr[arr.length/2-1])*0.5; return arr[arr.length/2];}
0 0
- Leetcode_Median of Two Sorted Arrays
- LeetCode_Median of Two Sorted Arrays
- LeetCode_Median of Two Sorted Arrays
- leetcode_Median of Two Sorted Arrays
- LeetCode_Median of Two Sorted Arrays
- leetcode_median of two sorted arrays
- **Leetcode_median-of-two-sorted-arrays (c++ and python version)
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of two sorted arrays
- Intersection of two sorted arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- [译]Front-end-Developer-Interview-Questions
- 《Objective-C基础教程》第四章 继承
- poj1753 Flip Game DFS,枚举
- Best Time to Buy and Sell Stock II
- [Learning Linux For Fun] unit 1 1.2~1.3
- leetcode_Median of Two Sorted Arrays
- LeetCode(116) Populating Next Right Pointers in Each Node
- 智慧城市
- UE4 移动平台游戏开发
- iOS 9之分屏多任务(Split View)
- POJ 1511 SPFA 模板
- C++对象模型——对象成员的效率 (Object Member Efficiency)(第三章)
- cocos2d-x 2.0版本 自适应屏幕分辨率
- poj 1035 Spell checker