LeetCode4 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)). (Hard)
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 分析:
利用寻找第K个元素的辅助函数。
二分搜索的思想,每次尽量去掉数组中的一部分元素(一半左右);
一次取K个元素出来,nums1中取 K/2个(不够就全都取出), nums2中取 K - K/2(或nums1.size()),
判断取出的两个数组元素中的末位谁大谁小;
如果nums1[p1] < nums2[p2],说明nums1取少了,nums2取多了,第K个元素应该在nums1的后半部分或nums2的前半部分;
如果nums1[p1] > nums2[p2], 说明nums2取少了,nums1取多了,第K个元素应该在nums2的后半部分或nums1的前半部分;
递归求解即可 。边界条件是nums1或nums2为空或K为1; 注:题目思路不是很难想到,但是处理细节有很多容易错的地方。
1.首先应确定Kth是第K个元素,对应下标应该为K-1
2.数组下标,取1/2位置时对应的哪个位置等等要注意,可以采用走样例的方式保证不出错。
3.采用了两种实现方法,一种是拷贝vector,要传参的时候注意左闭右开;
另一种是不拷贝vector,多传两个起始位置start,注意每次取元素操作时不能忘记start
第二个效率稍高,但编码中可能出错的地方也多一点。 代码1:
class Solution {
private:
double findKth(vector<int>& nums1, vector<int>& nums2, int K) { //第K个,对应下标K-1
if (nums1.size() > nums2.size()) {
return findKth(nums2,nums1,K);
}
if (nums1.size() == ) {
return nums2[K-];
}
if (nums2.size() == ) {
return nums1[K-];
}
if (K == ) {
return min(nums1[], nums2[]);
}
int s = nums1.size();
int p1 = min( K / , s);
int p2 = K - p1;
if (nums1[p1 - ] < nums2[p2 - ]) { //说明nums1取少了,kth在nums1后半段或nums2前半段
vector<int> n1(nums1.begin() + p1, nums1.end());
vector<int> n2(nums2.begin(), nums2.begin() + p2);
return findKth(n1, n2, K - p1);
}
else {
vector<int> n3(nums1.begin(), nums1.begin() + p1);
vector<int> n4(nums2.begin() + p2, nums2.end());
return findKth(n3, n4, K - p2);
}
}
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int s1 = nums1.size(), s2 = nums2.size();
int mid = (s1 + s2) / ;
if ( (s1 + s2) % == ) {
return (findKth(nums1, nums2, mid) + findKth(nums1, nums2, mid + )) / 2.0;
}
else {
return findKth(nums1, nums2, mid + );
}
}
};
代码2:
class Solution {
private:
double findKth(vector<int>& nums1, vector<int>& nums2, int K ,int start1, int start2) { //第K个,对应下标K-1
if (nums1.size() - start1 > nums2.size() - start2) {
return findKth(nums2,nums1,K,start2,start1);
}
if (nums1.size() - start1 == ) {
return nums2[start2 + K - ];
}
if (nums2.size() - start2 == ) {
return nums1[start1 + K - ];
}
if (K == ) {
return min(nums1[start1], nums2[start2]);
}
int s = nums1.size() - start1;
int p1 = min( K / , s);
int p2 = K - p1;
if (nums1[start1 + p1 - ] < nums2[start2 + p2 - ]) { //说明nums1取少了,kth在nums1后半段或nums2前半段
return findKth(nums1, nums2, K - p1, start1 + p1,start2);
}
else {
return findKth(nums1, nums2, K - p2, start1, start2 + p2);
}
}
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int s1 = nums1.size(), s2 = nums2.size();
int mid = (s1 + s2) / ;
if ( (s1 + s2) % == ) {
return (findKth(nums1, nums2, mid,,) + findKth(nums1, nums2, mid + ,,)) / 2.0;
}
else {
return findKth(nums1, nums2, mid + ,,);
}
}
};
其他与二分搜索相关的问题可以参考:
http://www.cnblogs.com/wangxiaobao/p/4915853.html
LeetCode4 Median of Two Sorted Arrays的更多相关文章
- Leetcode4:Median of Two Sorted Arrays@Python
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- leetcode4 Median of Two Sorted Arrays学习记录
学习了扁扁熊的题解:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/4-xun-zhao-liang-ge- ...
- Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- [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 ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)
转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- No.004 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...
随机推荐
- C#获取文件的绝对路径
要在c#中获取路径有好多方法,一般常用的有以下五种: //获取应用程序的当前工作目录. String path1 = System.IO.Directory.GetCurrentDirectory() ...
- Quora 用了哪些技术(转)
原文:http://dbanotes.net/arch/quora_tech.html 很多团队都在学习.研究 Quora .前段时间看到这篇 Quora’s Technology Examined ...
- (mac)Android Studio安装以及Fetching android sdk component information超时的解决方案
解决Mac下面Fetching android sdk component information加载过久问题, 关于windows中可以参考前面一篇文章 关于安装和下载可以百度一下地址.安装完成后, ...
- iPhone 6/6 Plus 出现后,如何改进工作流以实现一份设计稿支持多个尺寸?
iPhone 6/6 Plus 出现后,如何改进工作流以实现一份设计稿支持多个尺寸? 2014-12-05 09:33 编辑: suiling 分类:iOS开发 来源:知乎(pigtwo) 2 22 ...
- LightOJ 13361336 - Sigma Function (找规律 + 唯一分解定理)
http://lightoj.com/volume_showproblem.php?problem=1336 Sigma Function Time Limit:2000MS Memory L ...
- MySQL 查看表结构简单命令。
一.简单描述表结构,字段类型 desc tabl_name; 显示表结构,字段类型,主键,是否为空等属性,但不显示外键. 二.查询表中列的注释信息 select * from information_ ...
- UVaLive 6693 Flow Game (计算几何,线段相交)
题意:给个棋盘,你可以在棋盘的边缘处放2个蓝色棋子2个黄色棋子,问连接2组同色棋子的最小代价,如果线路交叉,输-1. 析:交叉么,可以把它们看成是两条线段,然后如果相交就是不行的,但是有几种特殊情况, ...
- 获取父进程ID
本程序主要功能是:获取某程序的ParentProcessID 直接上代码: // parent.cpp (Windows NT/2000) // // This example will show t ...
- chrome内核浏览器缓存资源找回方法
曾几何时,用chrome浏览器看了某个图片,网页,视频等,当时没保存,后来再怎么找都找不到了,chrome还把缓存加密了,不能像ie那样找回,这世上有买后悔药的吗?还真有! 搜索chromeCache ...
- Oracle数据库多语言文字存储解决方案
一.关于字符集 字符集(也称字元集,Character Set)就是字符编码表(codepage),一个字符不论英文.中文.韩文等在计算机系统内存或硬盘中通过二进制的字节(Byte)保存,这个二进制的 ...