[LeetCode] 4. Median of Two Sorted Arrays(想法题/求第k小的数)
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]
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小的数)的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — 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】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 ...
- 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之 median of two sorted arrays
这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...
- [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 ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- 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 ...
- 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 ...
随机推荐
- FZUOJ-2273 Triangles
Problem 2273 Triangles Accept: 109 Submit: 360 Time Limit: 1000 mSec Memory Limit : 262144 KB ...
- Codeforces - 1198D - Rectangle Painting 1 - dp
https://codeforces.com/contest/1198/problem/D 原来是dp的思路,而且是每次切成两半向下递归.好像在哪里见过类似的,貌似是紫书的样子. 再想想好像就很显然的 ...
- 垂直口风琴菜单3(jquery)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- python学习笔记(10):面向对象
一.类和实例 1.类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 2.对象:通过类定义的数据结构实例.对象包括两个数据成员( ...
- java 企业 网站源码 后台 springmvc SSM 前台 静态化 代码生成器
前台: 支持四套模版, 可以在后台切换 系统介绍: 1.网站后台采用主流的 SSM 框架 jsp JSTL,网站后台采用freemaker静态化模版引擎生成html 2.因为是生成的html,所以访问 ...
- Storm分布式集群搭建
一.storm版本 选用storm0.9.6 二.本地模式 用于对storm业务逻辑的调试和测试,可以直接在本地运行. 三.分布式模式 生产环境,需要对应的zookeeper.nimbus.super ...
- weakHashMap 用法
WeakHashMap,此种Map的特点是: 当除了自身有对key的引用外,此key没有其他引用,那么GC之后此map会自动丢弃此值 当使用 WeakHashMap 时,即使没有显示的添加或删除任何元 ...
- 总结const、readonly、static三者的区别【收藏、转载】20190614
总结const.readonly.static三者的区别 const:静态常量,也称编译时常量(compile-time constants),属于类型级,通过类名直接访问,被所有对象共享! a.叫编 ...
- 学Python的第四天
第四天啦,今天依旧代码少的啃树皮.... 但是作业留的有了幼儿园的水准!!!让小编我看到了希望.... #!/usr/bin/env python # -*- coding:utf8 -*- impo ...
- tomcat的跳转与日志
1.跳转的关键性配置; 2. 日志的配置 1.跳转的关键性配置 当用户访问http://www.a.com/test时,会跳转打开/var/www/html目录下的页面 关键性配置如下: [root@ ...