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

 1 class Solution {
 2 public:
 3
 4     typedef vector<int>::iterator it;
 5
 6     int findKth(it seq1,int size1,it seq2,int size2,int k)
 7     {
 8         if(size1==0)    return seq2[k-1];
 9         if(size1>size2)    return findKth(seq2,size2,seq1,size1,k);
10         if(k==1)    return min(*seq1,*seq2);
11
12         int p1=min(size1,k/2);    //保证p1<=p2
13         int p2=k-p1;
14
15         if(seq1[p1-1]<seq2[p2-1])
16             return findKth(seq1+p1,size1-p1, seq2,size2, k-p1);
17         else
18             return findKth(seq1,size1, seq2+p2,size2-p2, k-p2);
19     }
20
21
22     double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
23         int n=nums1.size(), m=nums2.size();
24         if((n+m)&1)
25             return findKth(nums1.begin(),n, nums2.begin(),m, (n+m+1)/2);
26         else
27         {
28             double a=findKth(nums1.begin(),n, nums2.begin(),m, (n+m)/2);
29             double b=findKth(nums1.begin(),n, nums2.begin(),m, (n+m)/2+1);
30             return (a+b)/2;
31         }
32     }
33 };

AC代码

时间: 2024-08-10 23:28:56

LeetCode Median of Two Sorted Arrays 找中位数(技巧)的相关文章

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 sorted arrays. The overall run time complexity should be O(log (m+n)). 有两个已排序的数组A和B,大小为m 和 n. 找出两数组的中位数 时间复杂度应该为 O(log (m+n)). 简单粗暴的方法就是把两个数组合并成一个数组,排序,取中位数.

[leetcode]Median of Two Sorted Arrays @ Python

原题地址:https://oj.leetcode.com/problems/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 arrays. The overall run time complexity should be O(log (m+n)). 解题思路:这道题要求两个已经排好

[LeetCode] Median of Two Sorted Arrays [16]

题目 There are two sorted arrays A and B 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)). 原题链接(点我) 解题思路 返回两个排序数组的中位数.这个题可以有以下几个思路: 首先可以想到的是将两个数组merge起来,然后返回其中位数. 第二个是,类似merg

LeetCode—Median of Two Sorted Arrays

Median of Two Sorted Arrays 这道题要找Median,中位数.这个是指,如果数组大小是偶数,返回中间两个数的平均值,如果是奇数个,就是中间的数. 算法时间效率要求是 O(log(m + n)),具体思路网上都一样. 另外,现在leetCode的C++ 数组都换成vector了,所以只好整理一下vector的用法. 参考:http://imatlab.lofter.com/post/286ffc_a81276 http://www.cnblogs.com/wang7/ar

leetcode | Median of Two Sorted Arrays 寻找2个有序数组中第k大的值

问题 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 arrays. The overall run time complexity should be O(log(m + n)). 分析 本题更经典通用的描述方式时: 给定2个有序数组,找出2个数组中所有元素中第k大的元素. 思路1 直观思

[JS做LeetCode] Median of Two Sorted Arrays

Median of Two Sorted Arrays <html> <head> <meta http-equiv="charset" content="utf-8"></head> <body> <script> //解题思路: //找出中位数要循环的次数为loop //posA为数组A的下标,posB为数组B的下标 //每次循环找出两个数组对应下标比较小的值,进栈,下标+1 //注意判断数

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 sorted arrays. The overall run time complexity should be O(log (m+n)). 题意: 两个排序后的数组nums1 和nums2,长度分别是m,n,找出其中位数,并且时间复杂度:O(log(m+n)) 最愚蠢的方法: 两个数组合

[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 sorted arrays. The overall run time complexity should be O(log (m+n)). http://www.acmerblog.com/leetcode-median-of-two-sorted-arrays-5330.html http:/

[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 sorted arrays. The overall run time complexity should be O(log (m+n)). Subscribe to see which companies asked this question Show Tags 分析: 这个题目在leetco