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

本文提供了一种求解两个已排序数组中位数的方法,通过迭代比较两个数组的元素来找到中间位置的数值,实现了对LeetCode经典题目“Median of Two Sorted Arrays”的解答。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

	}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值