两个有序的数组 nums1 和 nums2 维数分别为m,n。找所有数的中位数,复杂度 O(log (m+n))

注意:奇偶个数,分治法求解,递归出口特殊处理。取Kth smallest数时,分治取mid=k/2和k-mid,避免奇数造成影响。

 class Solution {
double findKth(vector<int> num1,vector<int> num2, int k)
{
int m = num1.size(), n = num2.size();
if(m > n)
return findKth(num2,num1,k);
if(m == )
return num2[k-];
if(k == )
return num1[]>num2[]?num2[]:num1[];
int mid1=min(k/,m), mid2=k-mid1;
if(num1[mid1-]>num2[mid2-])
return findKth(num1,vector<int>(num2.begin()+mid2,num2.end()),k-mid2);
else if(num1[mid1-]<num2[mid2-])
return findKth(vector<int>(num1.begin()+mid1,num1.end()),num2,k-mid1);
else
return num1[mid1-]; } public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int m = nums1.size(), n = nums2.size();
return (findKth(nums1, nums2, (m + n + ) / ) + findKth(nums1, nums2, (m + n + ) / )) / 2.0;
}
};

LeetCode 4. Median of Two Sorted Arrays (分治)的更多相关文章

  1. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  2. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

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

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

  4. LeetCode 4. Median of Two Sorted Arrays & 归并排序

    Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...

  5. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

  6. 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)

    4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...

  7. Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)

    4. Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n resp ...

  8. LeetCode 004 Median of Two Sorted Arrays

    题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...

  9. 【leetcode】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 s ...

随机推荐

  1. FCN 32分析:

    FCN 32s

  2. 使用wcf的双工模式做的一个控制台聊天app

    //wcf 服务 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Ser ...

  3. .net 重新注册

    今天同事问 一个IIS 的监控站点 .net 出现问题:对于windows 一般都停留在重启生效思想:然并没有生效: 于是建议重新注册.NET : 一般出现原因: 在默认安装路径 重启注册: 默认的安 ...

  4. 安卓中location.href或者location.reload 不起作用

    链接:https://www.cnblogs.com/joshua317/p/6163471.html 在移动wap中,经常会使用window.location.href去跳转页面,这个方法在绝大多数 ...

  5. 【转贴】Linux下MySQL 5.5的修改字符集编码为UTF8(彻底解决中文乱码问题)

    原文地址; http://www.ha97.com/5359.html PS:昨天一同事遇到mysql 5.5中文乱码问题,找我解决.解决了,有个细节问题网上没人说,我就总结一下. 一.登录MySQL ...

  6. 运维数据库平台~inception审核规则详解

    ---恢复内容开始--- 一 简介:这次我们来介绍最核心的审核功能 二 讲解:简单来说 inception就是mysql的二次过滤,何谓二次过滤,我们知道,mysql本身都有自己的审核规则,为业界所通 ...

  7. python - 练习(获取windows硬件信息)

    import subprocess import re # info = subprocess.Popen("systeminfo",shell=True,stdout=subpr ...

  8. java 多线程面试

    一.多线程的创建 1.多线程的创建 (1).继承Thread类 (2).实现Runnable接口 2.两种启动线程方法的区别 1.共同点 必须调用Thread 产生线程,然后调用start()方法 开 ...

  9. Java 集合和映射表

    集合 可以使用集合的三个具体类HashSet.LinkedHashSet.TreeSet来创建集合 HashSet类 负载系数 当元素个数超过了容量与负载系数的乘积,容量就会自动翻倍 HashSet类 ...

  10. python渗透

    计划写一个获取qq空间加密相册的工具. 分析: 她的相册密码是手机号,先写一个生成手机号的脚本 空间有她之前的手机号,那么她现在的手机号也极有可能是一样的运营商,比如移动(缩小密码范围) 自己新建一个 ...