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 ...
随机推荐
- 四种常见的 POST 提交数据方式(application/x-www-form-urlencoded,multipart/form-data,application/json,text/xml)
四种常见的 POST 提交数据方式(application/x-www-form-urlencoded,multipart/form-data,application/json,text/xml) 转 ...
- Debian/Ubuntu 下网易云音乐无法由图标/列表 打开的解决方案
前言 本文为使用图标或快捷方式直接打开网易云音乐的一个从安装说起的到解决问题的简单教程 环境 debian 9.5 理论上使用apt包管理器的发行版均支持 源使用国内的源即可,无需特殊指定 安装网易云 ...
- 使用Eclipse Memory Analyzer 进行JAVA内存泄露分析
一,安装 Eclipse Memory Analyzer 在Memory Analyzer的官网找到 update site的地址:
- LINQ to SQL 实现 CASE WHEN THEN 语句
Ø 前言 没有什么特别的,只是觉得 LINQ 的功能其实还是蛮强大的,所以简单记录下,算是工作笔记吧,有可能还能帮助到其他同学呢^_^. Ø 下面主要使用了 C# 三元运算符实现实现 SQL 中的 ...
- dp题
1.luogu 1484种树 50分思路:dp,但是数据规模过大没法dp选择奇怪贪心 dp方程 到i坑种j树 dp[i][j]=max(dp[i-1][j],dp[i-2][j-1]) 100分思路: ...
- tarjan,树剖,倍增求lca
1.tarjan求lca 思想: void tarjan(int u,int f){ for(int i=---){//枚举边 if(v==f) continue; dfs(v); //继续搜 uni ...
- HDU4738 Caocao's Bridges【强连通】
题意: 曹操有N个岛,这些岛用M座桥连接起来,每座桥有士兵把守(也可能没有),周瑜想让这N个岛不连通,但只能炸掉一座桥,并且炸掉一座桥需要派出不小于守桥士兵数的人去,桥的守兵数为0时,也需要派出一个人 ...
- break case
#include<stdio.h> main() { ; switch (g){ : : printf("haha"); break; : printf("h ...
- saltstack系列~第四篇
简介 针对mysql的sls编写0 软件包推送部分 tool_rsync: file.recurse: - source: salt://files/mysql ...
- Android常用网络请求框架Volley Retrofit (okHttp)
Android系统中主要提供了两种方式来进行HTTP通信,HttpURLConnection和HttpClient.在 Android 5.0 的时候 Google 就不推荐使用 HttpClient ...