`

<找工作 六>leetcode Median of Two Sorted Arrays

 
阅读更多

http://www.leetcode.com/onlinejudge

Median of Two Sorted Arrays

代码风格很乱,将就看吧,

写完才发现,写的有问题,没必要对end做操作,只操作start就可以了,类似归并

 

public class testMedianofTwoSortedArrays {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int A[]={};
		int B[]={2,3};
		double c=new testMedianofTwoSortedArrays().findMedianSortedArrays(A, B);
		System.out.println(c);
	}
	
	    public double findMedianSortedArrays(int A[], int B[]) {
	        // Start typing your Java solution below
	        // DO NOT write main() function        
	        int startA=0;
	        int startB=0;
	        int tempS=0,tempE=0;
	        int endA=A.length-1;
	        int endB=B.length-1;
	        double result=0;
	        while(true){
	            
	            if(startA>=A.length){
	                tempS=B[startB];
	                startB++;
	            
	            }else{
	            if(startB>=B.length){
	                tempS=A[startA];
	                startA++;
	            }
	            }
	            if(startA<A.length&&startB<B.length){
	            if(A[startA]>B[startB]){
	                tempS=B[startB];
	                startB++;
	            }else{                
	                tempS=A[startA];
	                startA++;
	            }
	            }
	            if(endA<0){
	                tempE=B[endB];
	                endB--;
	            }else{
	            if(endB<0){
	                tempE=A[endA];
	                endA--;
	            }
	            
	            }
	            if(endA>=0&&endB>=0){
	            if(A[endA]<B[endB]){
	                tempE=B[endB];
	                endB--;
	            }else{                
	                tempE=A[endA];
	                endA--;
	            }
	            
	            }
	            if (endA<startA&&endB<startB){
	                break;
	            }
	            
	            
	            
	        }
	        return ((double)tempS+tempE)/2;
	    
	    }
	}

 

改进后的,public class testMedianofTwoSortedArrays {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int A[] = {};
		int B[] = { 1, 2, 3, 4, 5 };
		double c = new testMedianofTwoSortedArrays().findMedianSortedArrays(A,
				B);
		System.out.println(c);
	}

	public double findMedianSortedArrays(int A[], int B[]) {
		// Start typing your Java solution below
		// DO NOT write main() function
		int startA = 0;
		int startB = 0;
		int tempS = 0, tempE = 0;
		double end = ((double) A.length + B.length) / 2;
		double result = 0;
		while (startA + startB < end) {
			if (startA >= A.length) {
				tempS = B[startB];
				startB++;
			} else if (startB >= B.length) {
				tempS = A[startA];
				startA++;
			} else if (A[startA] > B[startB]) {
				tempS = B[startB];
				startB++;
			} else {
				tempS = A[startA];
				startA++;
			}

		}
		if ((A.length + B.length) % 2 == 1) {
			tempE = tempS;

		} else if (startA >= A.length) {
			tempE = B[startB];

		} else if (startB >= B.length) {
			tempE = A[startA];

		} else if (A[startA] > B[startB]) {
			tempE = B[startB];

		} else {
			tempE = A[startA];

		}
		return ((double) tempS + tempE) / 2;

	}
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics