传送门

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. Win10系统下插入耳机前面板无声后面板有声的处理

    问题描述: 当耳机插入后面板绿色口(注意:耳机扬声器为绿色口,红色为话筒麦克风:前后面板一样):可以听到声音,但是转到前面板插入后,无声音:调出声音面板发现声音可随音度波动 处理步骤: 1.保证插牢接 ...

  2. Int、bigint、smallint、tinyint的区别

    Bigint:从-2^63-2^63的整型数据(所有数字).存储大小为8个字节.Bigint已经有长度了,在mysql建表中的length,只是用于显示的位数. Int:从-2^31-2^31的整型数 ...

  3. ant-design如果按需加载组件

    Ant Design React按需加载 Ant Design是阿里巴巴为React做出的组件库,有统一的样式及一致的用户体验 官网地址:https://ant.design 1.安装: npm in ...

  4. 基于 SwiftUI 创建一个可删除、可添加列表项的列表

    执行环境 macOS Mojave: 10.14.5 xcode: Version 11.0 beta 6 (11M392q) 预览效果 完整代码 import SwiftUI class Item: ...

  5. 网络拓扑_配置hybrid端口

    目的:实现不同VLAN间的PC不可互访, 但不同VLAN的PC均可以访问服务器. 例: VLAN5与VLAN10的PC不可以互通,但它们均可与服务器VLAN50互通. 拓扑图:

  6. Hugin

    Hugin简介 Hugin是一个开源的拼接软件,包含大量的拼接所需模块源码以及使用了部分Panorama Tools中的工具. 1)libpano13(Panorama Tools). 2)cpfin ...

  7. thinkphp 响应对象response

    1.可以通过修改配置文件的 default_return_type修改输出类型 // 默认输出类型 'default_return_type' => 'html', 2. 可以通过Config类 ...

  8. 关于BFC的总结

    虽然工作这么多年了,但是如果让我直接解释一下什么是BFC的时候,还是感觉有点不知道怎么准确的表达,下面就翻翻文档,总结一下,加深一下认识吧.大家也可以关注我的GitHub后续的更新 1.BFC的基本概 ...

  9. python数据库操作-mysql数据库

    一:连接 1:本地连接 mysql -u用户名 -p密码 2:连接远程服务器 mysql -u用户名 -p密码 -hip地址 -P端口号     线下修改远程服务端上部署的mysql服务器 二:创建数 ...

  10. URL编码表

    url编码是一种浏览器用来打包表单输入的格式. 定义 url编码是一种浏览器用来打包表单输入的格式.浏览器从表单中获取所有的name和其中的值 ,将它们以name/value参数编码(移去那些不能传送 ...