【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 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 两个有序数组中位数的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 【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 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...
- 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 ...
随机推荐
- 原生JS中unshift与shift
shift() 方法:shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值:用法:arrayObject.shift()如果数组是空的,那么 shift() 方法将不进行任何操作 ...
- win7 x64 eclipse_kepler下编译 hadoop1.1.2-celipse-plugin
1.编译前准备 a) 下载和解压hadoop-1.1.2.tar.gz b) 下载并配置ant 2.配置 a) 进入hadoop-1.1.2\src\contrib ...
- poj3535 A+B (大数加法)
A+B Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 811 Accepted: 371 Description The ...
- usb资料2
ubuntu linux下如何在启动时就关闭usb接口? https://zhidao.baidu.com/question/548651197.html Linux USB 驱动开发(四)—— 热插 ...
- linux下 C编程改变输出字体颜色
格式: echo "\033[字背景颜色;字体颜色m字符串\033[0m"例如:echo "\033[41;36m something here \033[0m" ...
- SCUT - 12 - 西方国家 - 矩阵快速幂
https://scut.online/p/12 可以用矩阵快速幂来做. #include<bits/stdc++.h> using namespace std; typedef long ...
- CSS 绝对定位与相对定位的区别
设置为绝对定位的元素框从文档流完全删除, 并相对于其包含块定位,包含块可能是文档中的另一个元素或者是初始包含块. 元素原先在正常文档流中所占的空间会关闭,就好像该元素原来不存在一样. 元素定位后生成一 ...
- 51nod1117【贪心】
思路:哈夫曼树~~哇塞,那么有道理. 利用堆维护:每次从堆里取两个最小加起来,然后还是最小的两个,最后只剩一根总的 #include <bits/stdc++.h> using names ...
- JS中的MOD运算
最近研究汉诺塔非递归的时候,看到书上写了个MOD,久违啊,感觉好久没看到过了,都忘了怎么用了. 某人:我知道,这不就是取余嘛,直接%就行了. 嗯......,如果是python语言,你说的很对,但是我 ...
- CC34:判断直线相交
题目 解法 水题,判断斜率.判断截距,ok..... class CrossLine { public: bool checkCrossLine(double s1, double s2, doubl ...