题4、Median of Two Sorted Arrays

题4、Median of Two Sorted Arrays

topic

There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
You may assume nums1 and nums2 cannot be both empty.

Example 1:

nums1 = [1, 3]
nums2 = [2]

The median is 2.0
Example 2:

nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5

Thinking

This question is given two rows of the array has been sorted, and then ask for a median of two arrays after the merger.

Problem-solving ideas is small to large contrast to the corresponding values, who will come to move forward a little more, and the current value has been compared (in contrast to over-value he is the greatest) and stored in median1 median2 in which the contrast is too median1 largest value, median2 is the second largest.

The reason why is because, according to store two different parity values, the median results are not the same (length is odd, the median one; even length, with a median of two mean), I set the number of cycles (length + 1) /2.0, that is if the length of the array is odd, then the median is exactly median1, is an even number, then the two values ​​is exactly the averaging of median1 and median2.

Note that the point is, the input array are empty, a blank, or a different length, and a variety of possible cross-border.
Anyway, all kinds of messy details, out of the pit noticed.

Code

public class T004 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int[] nums1 = { };
		int[] nums2 = {  };
		
		System.out.println(  findMedianSortedArrays(nums1,  nums2) );

	}
	
    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
    	
    	//两个数组的长度之和
    	int length = nums1.length + nums2.length;

    	//两个整型数,用于存贮中位数(长度为偶数的话中位数是两个的均值)
    	int median1 = Integer.MIN_VALUE;
    	int median2 = 0;
    	
    	//长度为零直接返回结果0
    	if( length == 0 )
    		return 0;
    	//长度为1直接返回结果
    	else if( length == 1 )
    		return nums1.length==0 ? nums2[0]:nums1[0];
    	
    	//判断给出的两个数组是否存在空的,存在的话直接得到中位数并返回结果
	    if( nums1.length == 0 && length > 1 ) {
			median1 = nums2[(length-1)/2];
			median2 = nums2[(length+1)/2];
			return (length+1)%2 == 0 ? median1 : (median1+median2)/2.0;
		}else if( nums2.length == 0 && length > 1 ) {
			median1 = nums1[(length-1)/2];
			median2 = nums1[(length+1)/2];
			return (length+1)%2 == 0 ? median1 : (median1+median2)/2.0;
		}
    	
    	for( int count = 0, i = 0, j = 0; count < (length+1)/2.0; count++ ) {
    		
    		//如果ij两个游标均未超出数组的长度
    		if( i<nums1.length && j<nums2.length ) {
    			
    			//判断当前的两个值的大小,并将小的存储在median1中,将median1之前的值的存储在median2中
    			if( nums1[i] < nums2[j] ) {
    				median2= median1;
    	    		median1 = nums1[i];
    				i++;
    			}else{
    				median2= median1;
    				median1 = nums2[j];
    				j++;
    			}
    		//若游标I超出
    		}else if( i>=nums1.length && j<nums2.length ) {
    			
    			//i对应数组nums1,若其超出就对比nums1最后一位(最大的一个)和nums2当前的,
    			//若大将其赋值给median1,小就对j进行加一并将nums2当前值赋给median1
    			if( nums1[nums1.length-1] > nums2[j] ) {
    				median2= median1;
    	    		median1 = nums1[nums1.length-1];
    				i++;
    			}else {
    				median2= median1;
    	    		median1 = nums2[j];
    				j++;
    			}
    		//若J超出
    		}else if( i<nums1.length && j>=nums2.length ) {
    			
    			//同上
    			if( nums1[i] < nums2[nums2.length-1] ) {
    				median2= median1;
    				median1 = nums2[nums2.length-1];
    				j++;
    			}else {
    				median2= median1;
    	    		median1 = nums1[i];
    				i++;
    			}
    			
    		}
			
    	}
    	
    	//根据奇偶返回相对应的结果
    	return (length+1)%2 == 0 ? median1 : (median1+median2)/2.0;
        
    }

}
Published 25 original articles · won praise 0 · Views 130

Guess you like

Origin blog.csdn.net/weixin_45980031/article/details/103465902