【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 ...
随机推荐
- eclipse中如何去除警告:Class is a raw type. References to generic type Class<T> should be parameterized
转自:https://blog.csdn.net/zwr_1022/article/details/78583872 解决前的源代码: public class test {public static ...
- Python模块学习——optparse
Python 有两个内建的模块用于处理命令行参数: 一个是 getopt,<Deep in python>一书中也有提到,只能简单处理 命令行参数: 另一个是 optparse,它功能强大 ...
- [poj1811]Prime Test(Pollard-Rho大整数分解)
问题描述:素性测试兼质因子分解 解题关键:pollard-rho质因数分解,在RSA的破译中也起到了很大的作用 期望复杂度:$O({n^{\frac{1}{4}}})$ #include<cst ...
- Apache2.2安装图解
Apache2.2安装图解 2010-12-14 15:32:44| 分类: 不学无术之杂 | 标签:安装 端口 httpd apache2.2 服务器 |字号 订阅 Apache音译 ...
- Spring:JdbcTemplate使用指南
Spring:JdbcTemplate使用指南 Spring:JdbcTemplate使用指南 前言: 本文指在介绍Spring框架中的JdbcTemplate类的使用方法,涉及基本的Spring反转 ...
- 泛型(Generic)
当集合中存储的对象类型不同时,那么会导致程序在运行的时候的转型异常 import java.util.ArrayList; import java.util.Iterator; public clas ...
- 第二课2、ROS
1.ROS框架 分为以下三个级别: 1)文件系统级 2)计算图级 计算图级是ROS处理数据的一种点对点的网络形式,描述程序是如何运行的. 基本的计算图级概念包括:节点,参数服务器,消息,服务,主题和包 ...
- ZOJ3469 Food Delivery
Food Delivery ZOJ - 3469 题意:外卖送饭给N个顾客,要求他们不满度和最小,没人不满度=等待时间*耐心值 #include<cstring> #include< ...
- 3、CreateJS介绍-SoundJS
需要在html5文件中引入的CreateJS库文件是soundjs-0.5.2.min.js HTML5文件如下: <!DOCTYPE html> <html lang=" ...
- CSS样式之操作属性一
********css之操作属性******** 一.文本 1.文本颜色:color 颜色属性被用来设置文字的颜色 颜色是通过CSS最经常的指定: 十六进制值 - 如: #FF0000 一个RGB值 ...