LeetCode Median of Two Sorted Arrays 找中位数(技巧)
题意:
给两个有序(升or降)的数组,求两个数组合并之后的中位数。
思路:
按照找第k大的思想,很巧妙。将问题的规模降低,对于每个子问题,k的规模至少减半。 考虑其中一个子问题,在两个有序数组中找第k大,我们的目的只是将k的规模减半而已,所以可以对比nums1[k/2]和nums2[k/2]的大小,假设nums1[k/2]<=nums2[k/2],那么nums1[0~k/2]这部分必定在0~k之中,那么将这部分删去,k减半,再递归处理。这里面可能有一些细节问题要考虑:
1. 如果其中1个数组的大小不及k/2。
2. 如果k=1了,两个数组依然还有元素,那么nums1[0]和nums2[0]必有一个为第k大。
3. 如果数组大小n+m是奇数还是偶数
4. 每个子问题的n大还是m大。
复杂度:k规模每次要么减半,要么就是其中一个数组不够k/2,那么递归到下一次就O(1)解决了。所以复杂度O( logk ),而k=(n+m)/2,所以O( log(n+m) )。
class Solution {
public:
typedef vector<int>::iterator it;
int findKth(it seq1,int size1,it seq2,int size2,int k)
{
if(size1==) return seq2[k-];
if(size1>size2) return findKth(seq2,size2,seq1,size1,k);
if(k==) return min(*seq1,*seq2);
int p1=min(size1,k/); //保证p1<=p2
int p2=k-p1;
if(seq1[p1-]<seq2[p2-])
return findKth(seq1+p1,size1-p1, seq2,size2, k-p1);
else
return findKth(seq1,size1, seq2+p2,size2-p2, k-p2);
}
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int n=nums1.size(), m=nums2.size();
if((n+m)&)
return findKth(nums1.begin(),n, nums2.begin(),m, (n+m+)/);
else
{
double a=findKth(nums1.begin(),n, nums2.begin(),m, (n+m)/);
double b=findKth(nums1.begin(),n, nums2.begin(),m, (n+m)/+);
return (a+b)/;
}
}
};
AC代码
LeetCode Median of Two Sorted Arrays 找中位数(技巧)的更多相关文章
- LeetCode: Median of Two Sorted Arrays 解题报告
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- 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 ...
- Median of Two Sorted Arrays (找两个序列的中位数,O(log (m+n))限制) 【面试算法leetcode】
题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...
- [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 ...
- LeetCode—— Median of Two Sorted Arrays
Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...
- C++ Leetcode Median of Two Sorted Arrays
坚持每天刷一道题的小可爱还没有疯,依旧很可爱! 题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. ...
- [leetcode]Median of Two Sorted Arrays @ Python
原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...
- Leetcode: Median of Two Sorted Arrays. java.
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- LeetCode——Median of Two Sorted Arrays
Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...
随机推荐
- chkconfig命令
chkconfig --list #列出系统所有的服务启动情况chkconfig --add xxx #增加xxx服务chkconfig --de ...
- [转载]Windows 7笔记本创建wifi热点供手机上网教程
用智能手机的朋友会发现这样一个问题,智能手机比普通手机上网更耗流量.这是因为智能手机应用(软件)丰富,而且大部分应用都会自动联网.为此,许多人每月包了上百M的流量套餐,但用的时候还是小心翼翼,生怕流量 ...
- 电脑的基本硬件知识以及unix图解
1.DELL R720 R610 2.电源: 人体心脏3.硬盘: 存数据的地方.机械的性能不高,3.5英寸 性能 SATA 借口<SAS <SSD 价格:SSD> SAS>SA ...
- C#学习7.31判断体重是否超标
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 用for循环打印菱形
package nothh; public class mmm { public static void main(String[] args) { //for循环内的 for按顺序运算,先打印1/4 ...
- bzoj 2744: [HEOI2012]朋友圈
#include<cstdio> #include<iostream> #define M 3010 using namespace std; ],u[M*M>>] ...
- Jmeter中的几个重要测试指标释义
一.基本概念 1.测试计划是使用 JMeter 进行测试的起点,它是其它 JMeter 测试元件的容器. 2.线程组:代表一定数量的并发用户,它可以用来模拟并发用户发送请求.实际的请求内容在Sampl ...
- 安装VMWare tools,以及解决安装后/mnt中有hgfs但没共享文件的方法
一.首先是安装VMWare tools 安装过程可参考:Installing VMware Tools in an Ubuntu virtual machine 安装成功后,可看的如下信息: ...
- 一模 (1) day2
第一题:(水题) 题目大意:就是给出扫雷的图,然后统计每个九宫格的雷的个数. 解题过程: 1.好久没做这样的水题了.直接模拟水过.. 第二题: 题目大意:给出一个长度小于1000的数k,要求一个尽可能 ...
- iOS9/iOS8界面对比 XCode7
Xcde7 bate 无需开发这账号(99¥)可以调试程序 目前是测试版 iOS9/iOS8界面对比 (注:左边为iOS8界面,右边为iOS9界面.) 1.新字体 苹果在 iOS9 中使用旧金山字体取 ...