LeetCode – Refresh – Median of Two Sorted Array

O(m + n):

Note:

return rec[1] not rec[0].

 1 class Solution {
 2 public:
 3     double findMedianSortedArrays(int A[], int m, int B[], int n) {
 4         int mid = (m+n)/2 + 1, i = 0, j = 0;
 5         vector<int> rec(2);
 6         while (i + j < mid) {
 7             int tmp1 = INT_MAX, tmp2 = INT_MAX;
 8             rec[0] = rec[1];
 9             if (i < m) {
10                 tmp1 = A[i];
11             }
12             if (j < n) {
13                 tmp2 = B[j];
14             }
15             if (tmp1 < tmp2) {
16                 rec[1] = tmp1;
17                 i++;
18             } else {
19                 rec[1] = tmp2;
20                 j++;
21             }
22         }
23         if ((m+n)%2 =
 1 class Solution {
 2 public:
 3     double findK(int A[], int m, int B[], int n, int k) {
 4         if (m > n) {
 5             return findK(B, n, A, m, k);
 6         }
 7         if (m == 0) {
 8             return B[k-1];
 9         }
10         if (k == 1) {
11             return min(A[0], B[0]);
12         }
13         int ia = min(m, k/2), ib = k - ia;
14         if (A[ia-1] < B[ib-1]) {
15             return findK(A+ia, m-ia, B, n, k-ia);
16         } else if (A[ia-1] > B[ib-1]) {
17             return findK(A, m, B+ib, n-ib, k-ib);
18         }
19         return A[ia-1];
20     }
21     double findMedianSortedArrays(int A[], int m, int B[], int n) {
22         if ((m + n)%2 == 1) {
23             return findK(A, m, B, n, (m+n)/2+1);
24         }
25         return (findK(A, m, B, n, (m+n)/2+1) + findK(A, m, B, n, (m+n)/2))/2;
26     }
27 };
= 1) return double(rec[1]);
24         return double(rec[0]+ rec[1])/2;
25     }
26 };

O(lg(m+n))

Note:

When do the comparison of A[ia-1] and B[ib-1], do not forget to return it.

时间: 2024-10-12 21:59:52

LeetCode – Refresh – Median of Two Sorted Array的相关文章

【Leetcode】Median of Two Sorted Array II

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)). 非常经典的一个问题,如果直接查找中位数,即偶数约定向下取整,奇数就是严格的中位数,这个题目直接可以比较每个数组的mid来计算得到. 但是这个问题的需求更改为如果为偶数,则中

LeetCode 4 Median of Two Sorted Array

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大的数. 自己想到的解决方法有一个先用合并排序将两个数组进行排序得到一个排列后的数组,在遍历找到第k到的数

Median of Two Sorted Array leetcode java

题目: 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)). 题解: 首先我们先明确什么是median,即中位数. 引用Wikipedia对中位数的定义: 计算有限个数的数据的中位数的方法是:把所有的同类数据按照大小的顺序排列

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】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

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小数的问题,原问题也得以解决

Java for LeetCode 081 Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 解题思路: 参考Java for LeetCode 033 Search in Rota

LeetCode Solutions : Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. Java Solution ( refer to my blog LeetCode So