4. Median of Two Sorted Arrays
来源:互联网 发布:网络之复仇剑士下载 编辑:程序博客网 时间:2024/06/18 17:15
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
求两个有序数组的中位数,O(m+n)的复杂度也能求解,而且还非常简单,但是题目要求时间复杂为O(log(m+n)),方法基本上可以锁定为二分、分治、归并类型算法。
求两个有序数组的中位数,其实可转化为求两个有序数组的第k个数(m+n为偶数时为k-1和k)。
假设两个数组均为升序,假设len_a <= len_b。求k采用分治算法,递归查找第k个数。
当a[k/2-1] < b[k/2-1]时,k/2-1对应数组的下标,所以k/2-1其实是对应数组的第k/2个数(第1个数下标为0),在a[k/2-1]<b[k/2-1]时,小于等于a[k/2-1]的数的数量<=k/2+k/2-1个,即k-1个,其中数组a有k/2个数(含a[k/2-1]),数组b最多k/2-1个数(因为不含b[k/2-1],且又不确定a[k/2-1]是否小于数组b中下标为k/2-1前面的数)。又可知第k个数不可能在b[k/2-1]的右边(不含b[k/2-1]),因为前面还有a的k/2个数和b的k/2个数,共k个数,此时b[k/2-1]是第k个数,与第k个数在b[k/2-1]右边矛盾了,因此当a[k/2-1] < b[k/2-1]情况下,第k个数一定位于a的右半部分和b的左半部分,根据该条件递归查找。
同理分析a[k/2-1] > b[k/2-1]。
当a[k/2-1] = b[k/2-1]时,根据第一种情况的分析,这个数肯定位于b[k/2-1]处。
AC代码如下:
class Solution { public int findMedianIndex(int[] nums1, int begin1, int len1, int[] nums2, int begin2, int len2, int k){ if (len1 > len2){ return findMedianIndex(nums2, begin2, len2, nums1, begin1, len1, k); } if (len1 == 0){ return nums2[begin2 + k-1]; } if (k == 1){ return Math.min(nums1[begin1], nums2[begin2]); } int part1 = Math.min(k/2, len1), part2 = k - part1; if (nums1[begin1 + part1 - 1] == nums2[begin2 + part2 - 1]){ return nums1[begin1 + part1 - 1]; } else if (nums1[begin1 + part1 - 1] < nums2[begin2 + part2 - 1]){ return findMedianIndex(nums1, begin1 + part1, len1 - part1, nums2, begin2, len2, k - part1); } else { return findMedianIndex(nums1, begin1, len1, nums2, begin2 + part2, len2 - part2, k - part2); } } public double findMedianSortedArrays(int[] nums1, int[] nums2) { int len1 = nums1.length, len2 = nums2.length, sumLen = len1 + len2; if (sumLen % 2 != 0) { return findMedianIndex(nums1, 0, len1, nums2, 0, len2, sumLen / 2 + 1); } else { return (findMedianIndex(nums1, 0, len1, nums2, 0, len2, sumLen / 2)+ findMedianIndex(nums1, 0, len1, nums2, 0, len2, sumLen / 2 + 1)) / 2.0; } }}
- [LeetCode]4.Median of Two Sorted Arrays
- LeetCode 4. Median of Two Sorted Arrays
- LeetCode --- 4. Median of Two Sorted Arrays
- [Leetcode] 4. Median of Two Sorted Arrays
- 4.Median of Two Sorted Arrays
- [LeetCode]4.Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- 4.Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- 【leetcode】4. Median of Two Sorted Arrays
- Leetcode-4.Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- LeetCode-4.Median of Two Sorted Arrays
- leetcoode 4. Median of Two Sorted Arrays
- Leetcode 4. Median of Two Sorted Arrays
- leetcode 4. Median of Two Sorted Arrays
- canvas实现简易时钟效果
- 面经笔经01_交替上浮下沉冒泡排序
- wincc
- POJ开餐馆
- JavaWeb中Tomcat的相关配置以及配置任意目录下的Web应用
- 4. Median of Two Sorted Arrays
- Dynamic Programming:70. Climbing Stairs
- 第4周项目3(2)
- 有关于python学习过程中实用的网站
- centos编译安装PHP7
- Java当前时间
- Gradle整合jetty的第一个HelloWorld工程
- Webservice03---dtd与Schema
- subsets