leetcode Median of Two Sorted Arrays
2015-04-30 21:32
211 查看
double inline mynext(int A[], int m, int a, int B[], int n, int b){ if (a >= m) return B[b]; if (b >= n) return A[a]; return A[a] < B[b] ? A[a] : B[b]; } class Solution { public: double findMedianSortedArrays(int A[], int m, int B[], int n) { int count = 0; int hand = 0; int next = 0; int xxx = (m + n + 1) / 2; int a = 0; int b = 0; while (a<m && b<n && count<xxx) { if (A[a]<B[b]) { hand = A[a]; a++; } else { hand = B[b]; b++; } count++; } if (count >= xxx) { if ((m + n) % 2 == 0) return (hand + mynext(A,m,a,B,n,b)) / 2.0; else return hand; } else if (a<m){ while (count<xxx) { hand = A[a++]; count++; } } else if (b<n){ while (count<xxx) { hand = B[b++]; count++; } } if ((m + n) % 2 == 0) return (hand +mynext(A,m,a,B,n,b)) / 2.0; else return hand; } };
相关文章推荐
- leetcode第4题——***Median of Two Sorted Arrays
- LeetCode.Problem 4 Median of Two Sorted Arrays
- LeetCode之Median of Two Sorted Arrays
- Leetcode Median of Two Sorted Arrays
- [leetcode ][4] Median of Two Sorted Arrays
- LeetCode Median of Two Sorted Arrays 在两个已排列的数组中找出中位数。时间复杂度为O(log(min(N,M))
- 记LeetCode_4: Median of Two Sorted Arrays
- leetcode Median of Two Sorted Arrays
- LeetCode 4. Median of Two Sorted Arrays
- Leetcode_Problem4_Median of Two Sorted Arrays(两种解法)
- leetcode 4 golang Median of Two Sorted Arrays
- leetcode题解||Median of Two Sorted Arrays问题
- 【leetcode】之Median of Two Sorted Arrays
- 【LeetCode】Median of Two Sorted Arrays
- LeetCode之4_Median of Two Sorted Arrays
- Leetcode日记(4)---Median of Two Sorted Arrays
- leetCode之Median of Two Sorted Arrays
- LeetCode-Median of Two Sorted Arrays
- leetcode之Median of Two Sorted Arrays
- [leetcode]Median of Two Sorted Arrays