LeetCode-Median of Two Sorted Arrays
来源:互联网 发布:js多选日期插件 编辑:程序博客网 时间:2024/06/17 06:47
像我这种算法渣渣估计就会先排序跑过再说,,o(╯□╰)o。。
考虑时间复杂度,二分查找结合递归才是正道。代码如下,思路已懂,,路过可自行度度。。
double findKth(int a[],int m,int b[],int n,int k){ //always assume that m is equal or smaller than n if(m>n) return findKth(b,n,a,m,k); if(m==0) return b[k-1]; if(k==1) return min(a[0],b[0]); //divide k into two parts int pa=min(k/2,m),pb=k-pa; if(a[pa-1]<b[pb-1]) return findKth(a+pa,m-pa,b,n,k-pa); else if(a[pa-1]>b[pb-1]) return findKth(a,m,b+pb,n-pb,k-pb); else return a[pa-1];}class Solution{ public: double findMedianSortedArrays(int A[],int m,int B[],int n) { int total=m+n; if(total%2!=0) return findKth(A,m,B,n,total/2+1); else return (findKth(A,m,B,n,total/2)+findKth(A,m,B,n,total/2+1))/2.0; }}
阅读全文
0 0
- LeetCode: Median of Two Sorted Arrays
- LeetCode Median of Two Sorted Arrays
- leetcode 26: Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- [Leetcode] 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
- [LeetCode] Median of Two Sorted Arrays
- [leetcode] Median of Two Sorted Arrays
- leetcode-004: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
- Android 支付宝调用测试版(沙箱环境)提示系统繁忙,请稍后再试(ALI40247)
- 常见PCB表面处理工艺简介
- USB 3G上网卡讲解之三
- HDU
- 排序算法的时间复杂度以及空间复杂度
- LeetCode-Median of Two Sorted Arrays
- HDU -2018 母牛的故事&&斐波那契兔子数列(函数递归)
- Ruby 中一些好用的方法(注意reduce方法)
- hdu1025 最长上升子序列 O(nlogn)优化版
- SpringBoot拦截全局异常并发送邮件给指定邮箱
- 秘传(加密)和签名的区别
- jQuery选择器要点
- HDU 6185 Covering
- 实现MVC: 3. AOP实现准备篇代理模式