Median of Two Sorted Arrays 有序数组A和B合并之后第k小的数
来源:互联网 发布:matlab 矩阵添加元素 编辑:程序博客网 时间:2024/04/30 22:40
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)).
class Solution {public://如果A[k/2-1]<B[k/2-1],那么A[0]~A[k/2-1]一定在第k小的数的序列当中,可以用反证法证明。//copy函数de用法//vector拷贝前要resize double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int m=nums1.size(),n=nums2.size(); int total=m+n; if(total & 1) return solve(nums1,m,nums2,n,total/2+1); else return (solve(nums1,m,nums2,n,total/2)+solve(nums1,m,nums2,n,total/2+1))*1.0/2; } int solve(vector<int> a,int m,vector<int> b,int n,int k) { if(m>n) return solve(b,n,a,m,k); if(m==0) return b[k-1]; if(k==1) return min(a[0],b[0]); int pa=min(k/2,m); int pb=k-pa; if(a[pa-1]<b[pb-1]) { vector<int> x; x.resize(m-pa); copy(a.begin()+pa,a.end(),x.begin()); return solve(x,m-pa,b,n,k-pa); } else if(a[pa-1]>b[pb-1]) { vector<int> x; x.resize(n-pb); copy(b.begin()+pb, b.end(),x.begin()); return solve(a,m,x,n-pb,k-pb); } else return a[pa-1]; }};
0 0
- Median of Two Sorted Arrays 有序数组A和B合并之后第k小的数
- LeetCode-4-Median-of-Two-Sorted-Arrays 递归求两有序数组第k小
- [LeetCode]Median of Two Sorted Arrays 二分查找两个有序数组的第k数(中位数)
- 复杂度为log(m+n)下求有序数组A和B有序合并之后第k小的数
- [leetcode]median of two sorted arrays【寻找第k小的数问题】
- leetcode | Median of Two Sorted Arrays 寻找2个有序数组中第k大的值
- LeetCoder 4. Median of Two Sorted Arrays (有序数组合并的中位数)
- 【数组&中位数&第K位数】Median of Two Sorted Arrays
- 两个有序数组的中位数 Median of Two Sorted Arrays
- Leetcode#4.Median of Two Sorted Arrays(归并排序&二分查找求第k小的数)
- 4. Median of Two Sorted Arrays 典型的二分,分治算法, 另 第k小的O(lgk)解法
- [leetcode] Median of Two Sorted Arrays 两个排序数组找出第k个数,或者中位数
- (leetcode)4.两个有序数列中间的第n个数 Median of Two Sorted Arrays--Java
- Median of Two Sorted Arrays 两个有序数组的中位数@LeetCode
- Median of Two Sorted Arrays 两个有序数组的中位数@LeetCode
- Median of Two Sorted Arrays - 寻找两个有序数组的中位数(重)
- LeetCode OJ 之 Median of Two Sorted Arrays(两个有序数组的中位数)
- [leetcode] Median of Two Sorted Arrays 寻找两个有序数组的中位数
- java各种数据库连接
- iOS Crash文件的解析(一、符号化Crash文件)
- 第五章:OGG 进程清除
- 动态规划 补充
- 黑马程序员- IO(Input- Output)(一)
- Median of Two Sorted Arrays 有序数组A和B合并之后第k小的数
- Java web高级JMX管理基础(一)
- 第四章:OGG Checkpoint 详解
- 20150615-OrCAD中自顶向下绘制层次原理图
- 浅谈 iOS 版本号
- JS中setTimeout()的用法详解
- android解决listview与scrollview的冲突、自定义listview的高度以及上下拉刷新
- Python的print命令使用中的一个小错误
- range()函数