传送门

Description

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)).

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

思路

题意:给定两个有序数组,在log级别的复杂度下,求得这两个数组中所有元素的中间值

题解:转换为求第k大的数。

假设A和B的元素个数都大于k/2,我们将A的第k/2个元素(即A[k/2-1])和B的第k/2个元素(即B[k/2-1])进行比较,有以下三种情况(为了简化这里先假设k为偶数,所得到的结论对于k是奇数也是成立的):

  • A[k/2-1] == B[k/2-1]
  • A[k/2-1] > B[k/2-1]
  • A[k/2-1] < B[k/2-1]

如果A[k/2-1] == B[k/2-1],意味着A[0]到A[k/2-1]的肯定在A∪B的top k元素的范围内,换句话说,A[k/2-1]不可能大于A∪B的第k大元素。

因此,我们可以放心的删除A数组的这k/2个元素。

同理,当A[k/2-1] > B[k/2-1]时,可以删除B数组的k/2个元素。

当A[k/2-1] == B[k/2-1]时,说明找到了第k大的元素,直接返回A[k/2-1]或B[k/2-1]即可。

因此,我们可以写一个递归函数。那么函数什么时候应该终止呢?

  • 当A或B是空时,直接返回B[k/2-1]或A[k/2-1];
  • 当k = 1时,返回min(A[0],B[0]);
  • 当A[k/2-1] ==B[k/2-1]时,返回A[k/2-1]或B[k/2-1]
 
C++:
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int len1 = nums1.size(),len2 = nums2.size();
int len = len1 + len2;
if (len & ){
return findKth(nums1,nums2,len / + );
} else{
return (findKth(nums1,nums2,len / ) + findKth(nums1,nums2,len / + ))/;
}
} double findKth(vector<int> nums1,vector<int> nums2,int k){
int len1 = nums1.size(),len2 = nums2.size();
if (len1 > len2) return findKth(nums2,nums1,k);
if (len1 == ) return nums2[k - ];
if (k == ) return min(nums1[],nums2[]);
int a = min(k / ,len1),b = k - a;
if (nums1[a - ] < nums2[b - ])
return findKth(vector<int>(nums1.begin() + a,nums1.end()),nums2,k - a);
else if (nums1[a - ] > nums2[b - ])
return findKth(nums1,vector<int>(nums2.begin() + b,nums2.end()),k - b);
else return nums1[a - ];
}
};

Java:

public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int len = nums1.length + nums2.length;
if ((len & ) == ){
return findKth(nums1,nums2,len / + );
}
else{
return (findKth(nums1,nums2,len / ) + findKth(nums1,nums2,len / + )) / ;
}
} public double findKth(int[] nums1, int[] nums2,int k){
int len1 = nums1.length,len2 = nums2.length;
if (len1 > len2) return findKth(nums2,nums1,k);
if (len1 == ) return nums2[k - ];
if (k == ) return Math.min(nums1[],nums2[]);
int a = Math.min(k / ,len1),b = k - a;
if (nums1[a - ] < nums2[b - ]) return findKth(Arrays.copyOfRange(nums1, a, len1),nums2,k - a);
else if (nums1[a - ] > nums2[b - ]) return findKth(nums1,Arrays.copyOfRange(nums2,b,len2), k - b);
else return nums1[a - ];
}
}

[LeetCode] 4. Median of Two Sorted Arrays(想法题/求第k小的数)的更多相关文章

  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】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 ...

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

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

  5. leetcode之 median of two sorted arrays

    这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...

  6. [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 two ...

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

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

  8. LeetCode题解-----Median of Two Sorted Arrays

    题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of t ...

  9. 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 ...

随机推荐

  1. 洛谷P2460 [SDOI2007]科比的比赛(题解)(贪心+搜索)

    科比的比赛(题解)(贪心+搜索) 标签:算法--贪心 阅读体验:https://zybuluo.com/Junlier/note/1301158 贪心+搜索 洛谷题目:P2460 [SDOI2007] ...

  2. uve (mui/light7)写APP的使用心得(大坑);

    话说mui这个框架的UI确实挺好看的(个人觉得)所以项目使用了他,结果里面的坑太TM多,不得不说MUI做东西太不用心了,社区不活跃,提问都没人管!; mui第一个坑: 日期选择器默认值无效:使用代码跟 ...

  3. python字符串学习总结

    python字符串是不可变类型 所以没有添加和删除操作,更改元素,不会更改元素本身,可以用id(str) 测试,只有从新赋值新的对象才有效果.

  4. P3188 [HNOI2007]梦幻岛宝珠

    传送门 注意到 $a,b$ 不大 考虑对每一个 $a*2^b$ 的 $b$ 分别背包 设 $f[i][j]$ 表示只考虑 $b=i$ 的物品时,容量为 $j= \sum a$ 的最大价值 这个就是普通 ...

  5. ofbiz idea 启动

    1.下载gradle并安装到本地 2.idea引入gradle 3.gradle右键选择refresh,项目会重新编译并加载gradle的task 4.可以再编译一下 5.没问题的话打开,jar ap ...

  6. rsync 和 inotify 结合

    我们知道 rsync 可以实现推送和拉取,而 inotify-tools 借助内核的 inotify 机制实现了文件的 实时监控.因此,借助这个思路,我们可以通过使用 shell 脚本,调整 inot ...

  7. 脚本_查找 Linux 系统中的僵尸进程

    #!bin/bash#功能:查找Linux系统中的僵尸进程#作者:liusingbon#使用awk判断ps命令输出的第8列为Z时,显示该进程的 PID 和进程命令ps aux |awk '{if($8 ...

  8. ShellExecute指定IE浏览器打开

    ShellExecute(NULL,L"open", L"iexplore.exe", L"www.baidu.com", NULL, SW ...

  9. 修改编码为utf8mb4 以支持emoji表情

    环境: 项目db的所有字符集都已经初始化为utf-8,如 status命令显示如下: Server characterset: utf8Db characterset: utf8Client char ...

  10. 图片公式转为word格式

    mathpix提取Latex格式,下载mathpix snipping tool工具,截图即可获取Latex格式公式 Latex格式去下面网站转换为mathml格式 https://johnmacfa ...