【LeetCode】median of two sorted arrays
来源:互联网 发布:vivo手机mac地址查询 编辑:程序博客网 时间:2024/05/16 18:21
题目:median of two sorted arrays
知识点:二分查找,中位数定义
public class Solution { /* * 关于:leetcode 第二题 给定两个排好序的数组,找到它们的中位数,要求:时间复杂度o(log(m+n)); * 变形:找第k个数 * 方法:1.遍历,时间复杂度为o(m+n),从头开始分别遍历两数组的数,按照大小计数,直到第k个数 * 2.递归,因为两个数组是排好序的,所以先各取第k/2的数比较,小的一边必然全都在第k个数之前(反证法) * 大的一边则可能不全在k前,于是递归再找剩下的k/2个数。 * 返回条件:1.一个数组为空时,直接返回另一个数组第k个数 * 2.k=1,返回min(A[0],B[0]) * 3.A[k/2-1] == B[k/2-1],返回其中一个的值 */ public double findMedianSortedArrays(int A[], int B[]) {int n = A.length;int m = B.length;int total = n+m;if(total%2 == 1){//如果两个数组的个数相加是奇数,不能有total&0x1判断。。return culMedian(A,n,B,m,total/2+1);}elsereturn (culMedian(A,n,B,m,total/2)+culMedian(A,n,B,m,total/2+1))/2.0; }public double culMedian(int A[],int n,int B[],int m,int k){if(n == 0)return B[k-1];if(m==0)return A[k-1];if(k == 1)return A[0]<B[0]?A[0]:B[0];int a = k/2 < n ? k/2 : n; //注意边界条件的考虑int b = k-a < m ? k-a : m;if(A[a-1] < B[b-1]){//java不能直接用A+a传递指针。。。还有别的方法吗??? int iA[] = new int[n-a];for(int i = a;i < n;i++)iA[i-a] = A[i]; return culMedian(iA,n-a,B,m,k-a);}else if(A[a-1] > B[b-1]){ int iB[] = new int[m-b];for(int i = b;i < m;i++)iB[i-b] = B[i];return culMedian(A,n,iB,m-b,k-b);}else return A[a-1]; }}
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
- iOS设备中WiFi、蓝牙和飞行模式的开启与关闭
- UVA - 11137 Ingenuous Cubrency
- TextView内容过长加省略号
- [150316]电路学习笔记
- 高手路过--菜鸟版系统安装==(图文安装教程)+(最新win7+win8系统)+系统工具
- 【LeetCode】median of two sorted arrays
- 在Eclipse下编译Android原生APK方法
- 创新实践(1)--爬虫的简单理解与java页面的简单抓取
- C++ SYN攻击源码
- Android学习笔记の三
- TCP/UDP HTTP md5
- Android下自定义IP控件
- Cocos知识储备
- 性能优化,多线程