LeetCode 4. Median of Two Sorted Arrays (分治)
两个有序的数组 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 (分治)的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了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 ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)
4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
- 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 ...
- 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 ...
- 【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 ...
随机推荐
- Python写日志
import logging import ResultFolder logger = logging.getLogger() logger.setLevel(logging.DEBUG) def C ...
- VUE优秀的组件库总结
VUE组件库 vux github ui demo:https://github.com/airyland/vux Mint UI 项目主页:http://mint-ui.github.io/#!/z ...
- WIN10下安装USB转串口驱动出现“文件的哈希值不在指定的目录”的解决办法
今天安装openJTAG驱动时出现“文件的哈希值不在指定的目录”,系统为WIN10专业版. 原因是驱动无数字签名,在WIN10中是不安全的驱动,所以显示哈希值不在范围内不能安装. 经查阅已经解决,发放 ...
- Linux cache 缓存过大
linux cache 缓存过大 : 除重启服务 之外:直接释放内存方式之一: 修改配置释放cached内存: echo > /proc/sys/vm/drop_caches
- 堆叠窗口QStackedWidget
经常将QStackedWidget和QListWidget或者QListView搭配使用 import sys from PyQt5.QtWidgets import QApplication, QW ...
- [C++]线性链表之顺序表<二>
/* @content 线性链表之顺序表 @date 2017-3-21 1:06 @author Johnny Zen */ /* 线性表 顺序表 链式表[带头指针/不 ...
- mysql 案例 ~ pt-io工具的使用
一 简介:如何使用pt-iopfile调查io具体信息二 目的:利用pt-iopfile分析mysql内部IO操作密集的文件,用以发现问题三 使用: pt-iopfile -p mysql_pid ...
- python 入门基础24 元类、单例模式
内容目录: 一.元类 二.单例模式 一.元类 1 什么是元类: 源自一句话:在python中,一切皆对象,而对象都是由类实例化得到的 class OldboyTeacher: def __init__ ...
- commons-lang3-3.2.jar中的常用工具类的使用
这个包中的很多工具类可以简化我们的操作,在这里简单的研究其中的几个工具类的使用. 1.StringUtils工具类 可以判断是否是空串,是否为null,默认值设置等操作: /** * StringUt ...
- Dubbo服务超时
服务消费者引用服务提供者的服务时可能由于网络原因导致长时间未返回相应,此时大量的线程将会阻塞,引起性能下降等问题.可以通过引入服务超时来解决该问题 服务超时指服务在给定的时间内未返回相应将立即终止该请 ...