经典题find median of two sorted array
来源:互联网 发布:抽奖软件大转盘 编辑:程序博客网 时间:2024/05/15 16:46
10/30
大概写了15分钟, 3个错, 两个typo, 一个参数,不合格!
public class Solution { public double findMedianSortedArrays(int A[], int B[]) { int lenA = A.length, lenB = B.length; int len = (lenA+lenB); if(len%2 == 1){ //err1: typo lenA for len return findKthInArray(len/2+1,A,0,lenA,B,0,lenB); }else{ return (findKthInArray(len/2,A,0,lenA,B,0,lenB) + findKthInArray(len/2+1,A,0,lenA,B,0,lenB))/2.0; } } public int findKthInArray(int k, int[] A, int startA, int lenA, int[] B, int startB, int lenB){ // note that lenA <= lenB if(lenA > lenB){ return findKthInArray(k,B,startB,lenB,A,startA,lenA); } // now lenA <= lenB if(lenA == 0) return B[startB+k-1]; else if(k==1) return Math.min(A[startA],B[startB]); int pA = Math.min(k/2, lenA); //pA <= k/2 <= pB int pB = k-pA; int checkA = startA + pA - 1; // err2: lenA for startA int checkB = startB + pB - 1; if(A[checkA] == B[checkB]) return A[checkA]; else if(A[checkA] < B[checkB]) return findKthInArray(k-pA,A, checkA+1, lenA-pA, B, startB, lenB); else return findKthInArray(k-pB, A, startA, lenA, B, checkB + 1, lenB-pB); // err3: forget about -pB and -pA }}
0 0
- 经典题find median of two sorted array
- Median of Two Sorted Array
- Median of two sorted array
- median of two sorted array
- median of two sorted array
- Median of two sorted array
- LeetCode2: Median of Two sorted array.
- LeetCode 4: Median Of Two Sorted Array
- Leetcode - Median of Two Sorted Array
- Median of Two Sorted Array---LeetCode
- [leetcode] #4 Median of Two Sorted Array
- LeetCode:Median of two sorted array
- Leetcode--Median of Two Sorted Array
- [Leetcode]Median of two sorted array
- [LeetCode]Median of Two Sorted Array
- Median of Two Sorted Array leetcode java
- Median of Two Sorted Array leetcode java
- array- Median of Two Sorted Arrays
- EventDispatch机制
- 【Leetcode】Reverse Integer
- 本院于2006年8月16日正
- 1,脏的功能——排泄 很
- 通过链表实现的多项式乘法【C++】
- 经典题find median of two sorted array
- cocos2dx3.2学习之路之基本概念
- 五月,记忆藏在石榴花下
- 有一个人、在我心里
- 用得真心,换的孑然心境
- 感谢对手
- 长春办理信用卡的微博_腾讯微博
- Android平台下的JNI开发
- [Leetcode]Valid Number