Leetcode Median of Two Sorted Arrays (java), leetcodemedian

Source: Internet
Author: User

Leetcode Median of Two Sorted Arrays (java), leetcodemedian

Solution 1:

Import java. util. arrays; class Solution {public double findMedianSortedArrays (int [] nums1, int [] nums2) {// store the merged array int [] nums3 = new int [nums1.length + nums2.length]; for (int I = 0; I <nums3.length; I ++) {if (I <nums1.length) {nums3 [I] = nums1 [I];} else {nums3 [I] = nums2 [i-nums1.length] ;}// sort Arrays in ascending order. sort (nums3); // search for median double median; if (nums3.length % 2 = 1) {median = nums3 [(nums3.length/2)];} else {median = (nums3 [(nums3.length/2)] + nums3 [(nums3.length/2)-1])/2.0;} return median ;}}

This solution combines two arrays and sorts them in ascending order to obtain the median value.

 

Solution 2:

Class Solution {public double findMedianSortedArrays (int [] nums1, int [] nums2) {int n = nums1.length; int m = nums2.length; // keep nums1 always shortest. // if num2 is longer than nums1, array out-of-bounds occurs. if (n> m) {return findMedianSortedArrays (nums2, nums1);} int l1; // int r1, the rightmost element in the left part of nums1; // int c1, the leftmost element in the right part of nums1; // The split position of nums1 is int l2; // int r2, the rightmost element in the left part of nums2; // int c2, the leftmost element in the right part of nums2; // split the part of nums2 // set the array to an odd int start = 0; int end = 2 * n + 1-1 ;/ /Start to continuously split nums1 while (start <= end) {// c1 c2 ensures that the total length of the array before m + n is 2 (m + n) c1 = (start + end)/2; c2 = m + n-c1; l1 = c1 = 0? Integer. MIN_VALUE: nums1 [(c1-1)/2]; r1 = c1> = 2 * n? Integer. MAX_VALUE: nums1 [(c1)/2]; l2 = c2 = 0? Integer. MIN_VALUE: nums2 [(c2-1)/2]; r2 = c2> = 2 * m? Integer. MAX_VALUE: nums2 [(c2)/2]; // l1 reduces r2 increase if (l1> r2) {end = c1-1 ;} // r1 increases l2 to reduce else if (l2> r1) {start = c1 + 1;} else {double median = (Math. max (l1, l2) + Math. min (r1, r2)/2.0; return median ;}} return 0.0 ;}}

The solution is as follows:

Split the two arrays into two parts. The two arrays are four parts: l1, r1, l2, and r2.

Because the value is the middle value, that is, the m + n (this is because the length of the array is assumed to be 2 * n + 1 to make the array an odd number)

The split position is first half of the first array, and the second array is the position of m + n-the first array, to ensure that the number of the left part of the two arrays is m + n

Make sure that the left half is always smaller than the right half, that is, l1 <r1 & l1 <r2 and l2 <r2 & l2 <r1 because it is already an ordered array, so half of them can be ignored, that is, l1 <r2 & l2 <r1

If the preceding conditions are not met, the split position will be moved. At this time, the correct position must be on one side. After the other side is removed, the split will continue until the above conditions are met.

When the above conditions are met, l1 and l2 are the largest, r1 and r2 are the smallest, and their sum is divided by 2 to get the answer.

Border Issues:

You only need to ensure nums1 <= nums2, because when nums1> nums2 is displayed, the nums2 split position may cross the array.

 

Github address: https://github.com/CyanChan/leetcode

  

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.