leetcode 4 Median of Two Sorted Arrays
来源:互联网 发布:sem与seo的区别 编辑:程序博客网 时间:2024/06/06 04:38
leetcode 4 Median of Two Sorted Arrays
Question: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)).
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {}
将本问题扩展为寻找两个有序序列的第k个数字。
所以有find_kth(int* nums1, int m, int* nums2, int n, int k )
注意:k从1开始计
所以有:
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) { int total = nums1Size + nums2Size; if(total & 0x1){//odd return find_kth(nums1, nums1Size, nums2, nums2Size, total/2); } else{//even return ( find_kth(nums1, nums1Size, nums2, nums2Size, total/2) + find_kth(nums1, nums1Size, nums2, nums2Size, (total/2)+1) )/2.0; }}
接下来的关键就是find_kth(int* nums1, int m, int* nums2, int n, int k )
怎么实现??
//// main.cpp// leetcode//// Created by YangKi on 15/11/06.// Copyright © 2015年 YangKi. All rights reserved.//#include<cstdlib>#include<iostream>using namespace std;int min(int a, int b){ return a<b? a : b;}int find_kth(int* A, int m, int* B, int n, int k)//k>=1{ if (m>n) return find_kth(B, n, A, m, k); if (m==0) return B[k-1]; if (k==1) return min(A[0], B[0]); //首先,是不能确定A的m大于k/2的,但能确定B的n一定大于k/2 int ia=min(m, k/2); //所以ia有两种取值 int ib = k-ia;//对应的ib希望能与ia凑出k个,则相应也有两个取值,这样每次两者的末尾才对应于第k个 if (A[ia-1] < B[ib-1]) { return find_kth(A+ia, m-ia, B, n, k-ia);// 因为此时已经删了ia个肯定是属于前k-1个的数字,则总的任务变成了找第k-ia大的数字 } else if( A[ia-1] > B[ib-1] ) { return find_kth(A, m, B+ib, n-ib, k-ib); } else { return A[ia-1]; }}double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size){ int total = nums1Size + nums2Size; if(total & 0x1) {//odd return find_kth(nums1, nums1Size, nums2, nums2Size, total/2+1); } else {//even return ( find_kth(nums1, nums1Size, nums2, nums2Size, total/2) + find_kth(nums1, nums1Size, nums2, nums2Size, (total/2)+1) )/2.0; }}int main(){ int A[3]={1,2,3}; int B[5]={4,5,6,7}; printf( "%lf\n", findMedianSortedArrays(A, 3, B, 4) ); return 0;}
1 0
- Leetcode 4 Median of Two Sorted Arrays
- LeetCode 4 - Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- [leetcode 4] Median of Two Sorted Arrays
- LeetCode 4:《Median of Two Sorted Arrays》
- [Leetcode] 4 - Median of Two Sorted Arrays
- leetcode|4|Median of Two Sorted Arrays
- [Leetcode]4Median of Two Sorted Arrays
- leetcode 4 Median of Two Sorted Arrays
- LeetCode #4 Median of Two Sorted Arrays
- LeetCode-4-Median of Two Sorted Arrays
- LeetCode 4 Median of Two Sorted Arrays
- leetcode 4 Median of Two Sorted Arrays
- LeetCode 4 Median of Two Sorted Arrays
- Leetcode[4] Median of Two Sorted Arrays
- LeetCode 4 - Median of Two Sorted Arrays
- leetcode 4 -- Median of Two Sorted Arrays
- MyBatis使用Collection查询多对多或一对多结果集bug
- hibernate与mybatis异同与比较(涵盖网上各个版本,清晰明了)
- 漫谈CSS transform动画技术
- iOS JPush极光推送
- leetcode15 3sum
- leetcode 4 Median of Two Sorted Arrays
- linux kernel内存碎片防治技术
- JQuery基础列表
- STM32F407+DP83848
- Xcode的一些使用技巧
- 拉普拉斯近似算法小结
- 【OpenCV】计算图像的水平和垂直积分投影
- python关于 date的使用
- 这几天的总结