004. Median of Two Sorted Arrays

 1 class Solution {
 2 public:
 3     double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
 4         const int m = nums1.size();
 5         const int n = nums2.size();
 6         int total = m + n;
 7         if (total & 0x1) return find_kth(nums1.begin(), m, nums2.begin(), n, total / 2 + 1);
 8         else
 9             return double(find_kth(nums1.begin(), m, nums2.begin(), n, total / 2) +
10                           find_kth(nums1.begin(), m, nums2.begin(), n, total / 2 + 1)) / 2;
11     }
12 //private:
13     int find_kth(vector<int>::const_iterator iter1, int len1, vector<int>::const_iterator iter2, int len2, int count)
14     {
15         if (len1 > len2) { // 这里比较重要
16             return find_kth(iter2, len2, iter1, len1, count);
17         }
18         else {
19             if (len1 == 0) return *(iter2 + count - 1);
20             if (count == 1) return min(*iter1, *iter2);
21             int m = min(len1, count / 2); // 这里的判定条件
22             int n = count - m;
23             if (*(iter1 + m - 1) < *(iter2 + n - 1)) return find_kth(iter1 + m, len1 - m, iter2, len2, count - m);
24             else if (*(iter1 + m - 1) > *(iter2 + n - 1)) return find_kth(iter1, len1, iter2 + n, len2 - n, count - n);
25             else return *(iter1 + m - 1);
26         }
27     }
28 };
时间: 2024-08-04 10:45:27

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

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,返回这个数. 否则,如

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

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

【题解】【数组】【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