[Leetcode-java] Median of Sorted Arrays

Source: Internet
Author: User

Topic:

There is sorted arrays nums1 and nums2 of size m and N respectively. Find The median of the sorted arrays. The overall run time complexity should be O (log (m+n)).

Ideas: The main idea is to find the median number of two arrays, check the relevant definition, the calculation of a finite number of data in the median method is: all the same data in the order of size. If the number of data is odd , then the middle data is the median of this group of data, if the number of data is even, then the median of the 2 data is the median of this group of data.

Share the online very concise idea of the algorithm, find two array of the K small element, the core of the method is to turn the original problem into a search for the K decimal point (assuming the two original sequence in ascending order), so the median is actually the first (m+n)/2 small number. So as long as the problem of k decimal is solved, the original problem is solved.

First, assuming that the number of elements of arrays A and b are greater than K/2, we compare a[k/2-1] and b[k/2-1] two elements, which represent the K/2 small element of a and the K/2 small element of B respectively. These two elements compare a total of three cases:>, < and =. If a[k/2-1]<b[k/2-1], this means that the elements of a[0] to a[k/2-1] are in the first K-small element after the merging of a and B. In other words, a[k/2-1] cannot be larger than the K-decimal value after the two array is merged, so we can discard it.

Code:

 Public classSolution { Public DoubleFindmediansortedarrays (int[] nums1,int[] nums2) {        intLen1 =nums1.length; intLen2 =nums2.length; intTotal = Len1 +Len2; if(total% 2 = = 1)            returnFindkth (nums1, 0, Len1-1, nums2, 0, len2-1, total/2+1); Else            return(Findkth (nums1, 0, Len1-1, nums2, 0, len2-1, TOTAL/2)                     + findkth (nums1, 0, Len1-1, nums2, 0, len2-1, total/2+1)/2; }         Public DoubleFindkth (int[] A,intAstart,intAend,int[] B,intbstart,intBendintk) {        intm = Aend-astart + 1; intn = bend-bstart + 1; if(M > N)//always keep A for the shortest easy follow-up judgment            returnfindkth (b, bstart, bend, A, Astart, Aend, K); if(M = = 0)//When there is an array that is empty            returnB[bstart + k-1];//Section K Small        if(k = = 1)//Find the smallest            returnmath.min (A[astart], B[bstart]); intda = Math.min (K/2, M);//when the length of M is less than K, the entire a array is required        intdb = K-da; if(A[astart + Da-1] < B[bstart + db-1]){            returnFindkth (A, Astart+da, aend, B, bstart, bend, K-da); }Else if(A[astart + da-1] > B[bstart + db-1]){            returnFindkth (A, Astart, Aend, B, bstart+db, Bend, K-db); }Else{            returnA[astart + da-1]; }    }}

Reference Link: http://blog.csdn.net/yutianzuijin/article/details/11499917/

Http://www.cnblogs.com/springfor/p/3861890.html

[Leetcode-java] Median of Sorted Arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.