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

详见:https://leetcode.com/problems/median-of-two-sorted-arrays/description/

Java实现:

方法一:常规解法

import java.util.Arrays;
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int size1=nums1.length;
int size2=nums2.length;
int size=size1+size2;
int[] nums = new int[size];
for(int i = 0; i<size1; i++){
nums[i] = nums1[i];
}
for(int i = size1; i<size; i++){
nums[i] = nums2[i-size1];
}
Arrays.sort(nums);
double median;
if((size-1)%2 == 0){
median = nums[(size-1)/2] * 1.0;
} else {
median = (nums[(size-1)/2] + nums[((size-1)/2)+1])/2.0;
}
return median;
}
}

 方法二:求第k小的数

public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int size1 = nums1.length;
int size2 = nums2.length;
int size = size1 + size2;
if(size % 2 == 1){
return findKth(nums1, 0, size1, nums2, 0, size2, size / 2 + 1);
}else{
return (findKth(nums1, 0, size1, nums2, 0, size2, size / 2) + findKth(nums1, 0, size1, nums2, 0, size2, size / 2 + 1)) /2;
}
}
public double findKth(int[] nums1, int start1, int size1, int[] nums2, int start2, int size2, int k){
if(size1 - start1 > size2 -start2){
return findKth(nums2, start2, size2, nums1, start1, size1, k);
}
if(size1 - start1 == 0){
return nums2[k - 1];
}
if(k == 1){
return Math.min(nums1[start1], nums2[start2]); // k==1表示已经找到第k-1小的数,下一个数为两个数组start开始的最小值
}
int p1 = start1 + Math.min(size1 - start1, k / 2); // p1和p2记录当前需要比较的那个位
int p2 = start2 + k - p1 + start1;
if(nums1[p1 - 1] < nums2[p2 - 1]){
return findKth(nums1, p1, size1, nums2, start2, size2, k - p1 + start1);
}else if(nums1[p1 - 1] > nums2[p2 -1]){
return findKth(nums1, start1, size1, nums2, p2, size2, k - p2 + start2);
}else{
return nums1[p1 - 1];
}
}
}

C++实现:

 class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int size1=nums1.size();
int size2=nums2.size();
int size=size1+size2;
if(size&0x1)
return findKTh(nums1.begin(),size1,nums2.begin(),size2,size/+);
else
return (findKTh(nums1.begin(),size1,nums2.begin(),size2,size/)+findKTh(nums1.begin(),size1,nums2.begin(),size2,size/+))/;
}
double findKTh(vector<int>::iterator nums1,int m,vector<int>::iterator nums2,int n,int k)
{
if(m>n)
return findKTh(nums2,n,nums1,m,k);
if(m==)
return *(nums2+k-);
if(k==)
return min(*nums1,*nums2);
int pa=min(k/,m),pb=k-pa;
if(*(nums1+pa-)<*(nums2+pb-))
return findKTh(nums1+pa,m-pa,nums2,n,k-pa);
else if(*(nums1+pa-)>*(nums2+pb-))
return findKTh(nums1,m,nums2+pb,n-pb,k-pb);
else
return *(nums1+pa-);
}
};

参考:

https://www.cnblogs.com/leavescy/p/5877627.html

https://www.cnblogs.com/bakari/p/5082155.html

http://blog.csdn.net/yutianzuijin/article/details/11499917/

004 Median of Two Sorted Arrays 两个有序数组的中位数的更多相关文章

  1. [LeetCode] 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 ...

  2. [LeetCode] 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 ...

  3. [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  4. Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...

  5. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  6. 【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  7. 【LeetCode】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 ...

  8. 4. Median of Two Sorted Arrays(2个有序数组的中位数)

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  9. Median of Two Sorted 求两个有序数组的中位数

    中位数是把一个数的集合划分为两部分,每部分包含的数字个数相同,并且一个集合中的元素均大于另一个集合中的元素. 因此,我们考虑在一个任意的位置,将数组A划分成两部分.i表示划分数组A的位置,如果数组A包 ...

随机推荐

  1. Bellman-Ford算法及其队列优化(SPFA)

    一.算法概述 Bellman-Ford算法解决的是一般情况下的单源最短路径问题.所谓单源最短路径问题:给定一个图G=(V,E),我们希望找到从给定源结点s属于V到每个结点v属于V的最短路径.单源最短路 ...

  2. mount error(12): Cannot allocate memory解决办法

    http://hi.baidu.com/zhangbin101004/item/e459f4d1f818dfbd33db903b 今天囧了啊,在ubuntu挂载的文件夹里面解压数据库,结果linux嫌 ...

  3. 问题4:对dict、list、tuple中的元素排序

    一)对字典中元素排序   方法一:利用sorted的key参数进行排序 from random import randint date = {k:randint(0, 20) for k in ran ...

  4. centos7 查看启动ntp服务命令

    标签(空格分隔): centos7 系统 1. 查看ntp服务命令: [root@node1 ~]# systemctl status ntpd * ntpd.service - Network Ti ...

  5. mysql数据库之表与表之间的关系

    表1 foreign key 表2 则表1的多条记录对应表2的一条记录,即多对一 利用foreign key的原理我们可以制作两张表的多对多,一对一关系 多对多: 表1的多条记录可以对应表2的一条记录 ...

  6. top查看CPU情况

    Linux查看CPU情况 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CPU 使用状况.运行 top ...

  7. Frequent Pattern 挖掘之二(FP Growth算法)

    Frequent Pattern 挖掘之二(FP Growth算法) FP树构造 FP Growth算法利用了巧妙的数据结构,大大降低了Aproir挖掘算法的代价,他不需要不断得生成候选项目队列和不断 ...

  8. GC偏好的校正与偏好程度的评估

    在二代测序仪上测出的数据,通常都会表现出测序深度与GC 含量的相关性,称为GC bias. GC bias校正 为了后续生物信息分析更加准确,通常需要做GC bias的校正. 2010 年 steve ...

  9. storm启动supervisor源码分析-supervisor.clj

    supervisor是storm集群重要组成部分,supervisor主要负责管理各个"工作节点".supervisor与zookeeper进行通信,通过zookeeper的&qu ...

  10. 23. CTF综合靶机渗透(十六)

    靶机说明: VM Name: JIS-CTF : VulnUpload Difficulty: Beginner Description: There are five flags on this m ...