LeetCode.004 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.找到这两个数组的中位数。

思路

令sum等于两个数组大小的和。

如果sum==1,返回这个数。

否则,如果sum是偶数,返回这两个数组中 第sum/2大 的数 和 第sum/2+1大 的数的均值;

如果sum是奇数,返回这两个数组中第sum/2+1大 的数。

 1 double find(int* nums1, int nums1Size, int* nums2, int nums2Size,int t){ //返回两个数组中第t小的数
 2     int pos1=0,pos2=0;
 3     int ans;
 4     while(t--)
 5     {
 6         if(pos1<nums1Size && pos2<nums2Size)
 7         {
 8             if(nums1[pos1]>nums2[pos2])
 9                ans=nums2[pos2++];
10             else
11                ans=nums1[pos1++];
12         }
13         else if(pos1<nums1Size)
14             ans=nums1[pos1++];
15         else if(pos2<nums2Size)
16             ans=nums2[pos2++];
17     }
18     return ans;
19 }
20 double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
21     int sum=nums1Size+nums2Size;
22     if(sum == 1)
23     {
24         if(nums1Size == 1)
25             return nums1[0];
26         else return nums2[0];
27     }
28     else
29     {
30         if(sum%2==0)
31             return (find(nums1,nums1Size,nums2,nums2Size,sum/2)+find(nums1,nums1Size,nums2,nums2Size,sum/2+1))/2;
32          else
33             return find(nums1,nums1Size,nums2,nums2Size,sum/2+1);
34     }
35 }
时间: 2024-10-25 20:27:20

LeetCode.004 Median of Two Sorted Arrays的相关文章

Java for LeetCode 004 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)). 解题思路: 由于要求时间复杂度O(log (m+n))所以几乎可以肯定是递归和分治的思想. 由于之前曾有过找两个数组第K小数的算法,时间复杂度为O(log(

LeetCode 004 Median of Two Sorted Arrays - Java

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,

[LeetCode] 004. Median of Two Sorted Arrays (Hard) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 004.Median_of_Two_Sorted_Arrays (Hard) 链接: 题目:https://oj.leetcode.com/problems/Median-of-Two-Sorted-Arrays/ 代码(github):https://github.com/illuz/leetcode 题意: 求

LeetCode(3) || Median of Two Sorted Arrays

LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题目内容 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

【题解】【数组】【Leetcode】Median of Two Sorted Arrays

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)). ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

LeetCode OJ - 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)). 解题思路: 将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数.所以只要解决了第k小数的问题,原问题也得以解决

[LeetCode][JavaScript]Median of Two Sorted Arrays

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)). https://leetcode.com/problems/median-of-two-sorted

第三周 leetcode 4. Median of Two Sorted Arrays (HARD)

4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二分算法求得两个序列中比它小的数,整体复杂度也是对数级别.但是代码实现较为困难. 换一个思路,我们把中位数不要当作一个数,而当作一个序列的划分.划分后,序列的左半部设为L,右半部设为R 满足max(L)<=min(R)且满足len(L)==len(R) 二分搜索这个划分即可.对于A+B的长度为奇数的情

leetcode 2.Median of Two Sorted Arrays

Median of Two Sorted Arrays Problem: 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)). 解题思路:如果是一个已排序数组,求中位数,可以直接计算.因此比较常规的想法是直接将两个有序数组合并