题目:

  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
public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int len1=nums1.length;
int len2=nums2.length;
int k=len1+len2; if(k%2!=0){
return findMedian(nums1,0,len1,nums2,0,len2,k/2+1);
}else{
return (findMedian(nums1,0,len1,nums2,0,len2,k/2)+findMedian(nums1,0,len1,nums2,0,len2,k/2+1))/2;
}
}
public static double findMedian(int[] nums1,int start1,int end1,int[] nums2,int start2,int end2,int k ){
if(end1>end2){
return findMedian(nums2,start2,end2,nums1,start1,end1,k);
} if(end1<=0){
return nums2[start2+k-1];
} if(k==1){
return Math.min(nums1[start1],nums2[start2]);
} int k1=Math.min(k/2,end1);
int k2=k-k1;
if(nums1[start1+k1-1]<nums2[start2+k2-1]){
return findMedian(nums1,start1+k1,end1-k1,nums2,start2,end2,k-k1);
}else{
return findMedian(nums1,start1,end1,nums2,start2+k2,end2-k2,k-k2);
}
}
}

  

【LeetCode】4.Median of Two Sorted Arrays 两个有序数组中位数的更多相关文章

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

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

  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. 004 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 ...

  5. 【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 ...

  6. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

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

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

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

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

  9. 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 ...

随机推荐

  1. fastreport整理

    Q1:如何直接打印,不显示打印对话框? frxReport1.PrintOptions.ShowDialog := false; frxReport1.PrepareReport(true); frx ...

  2. 下拉选择select和复选框checkbox的状态的各种方式

    复选框的状态 <input name="ck" value=" " type="checkbox"  checked> 或者&l ...

  3. CABasicAnimation动画及其keypath值和作用

    //tarnsform放大缩小动画 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transf ...

  4. OVS数据库操作

    说明 [Record]就是行对应的_uuid [if-exists]当值不存在的是否会报错而不是返回False 基本信息查询 列举数据库 # ovsdb-client list-dbs Open_vS ...

  5. Insert into a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  6. hihocoder1475 数组分拆【DP+前缀和优化】

    思路: DP[ i ] 代表以 i 结尾的方案数. dp[i] += sum[i] - sum[j - 1] != 0 ? dp[j] : 0 ; 对于100%的数据,满足1<=N<=10 ...

  7. 3dmax tcb控制器

    https://wenku.baidu.com/video/course/v/3a0e059d884c4d0b03bf85441b87311b 7.48开始 tcb控制器比较适合产生平滑动画 张力Te ...

  8. 《深入理解Java虚拟机》笔记04 -- 并发锁

    Java虚拟机在操作系统层面会先尽一切可能在虚拟机层面上解决竞争关系,尽可能避免真实的竞争发生.同时,在竞争不激烈的场合,也会试图消除不必要的竞争.实现这些手段的方法包括:偏向锁.轻量级锁.自旋锁.锁 ...

  9. jzoj6004. 【PKUWC2019模拟2019.1.17】集合 (组合数学)

    题面 题解 这种题目就是要好好推倒 我们枚举最小的数是哪一个,那么答案就是\[Ans=\sum_{i=1}^nT^i{n-i\choose k-1}\] 因为有\[\sum_{i=p}^n{n-i\c ...

  10. EOS Bios Boot Sequence

    EOS version:v1.0.5 Date:2018-06-19 Host: Centos 7 Reference :https://github.com/EOSIO/eos/wiki/Tutor ...