Algorithm combat (four) to find two ordered arrays median

I. Introduction

  Today began the fourth question, look for a median of two ordered arrays. Marking the difficulty of this problem leetcode is difficult, this may look scared me, was not done before difficult subject. But I go in, I feel quite simple title, but also a little happy for a moment, I thought that their level becomes higher. Results her face too quickly, a read drain conditions, the time required for the complexity O (log (n + m)), really is difficult. Ado. Look at the title.

II. Topic

  Title: Given two ordered arrays of size and nums1 m and n nums2.

     Please find both the median and orderly array, and requires time complexity of the algorithm is O (log (m + n)).

     You can not assume nums1 and nums2 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

III. Problem-solving ideas

  The title means it is easy to understand, it is to find the median after two numbers together, if is odd, then it is the middle of that, if it is even, the middle two dividing by 2;

  1) efflux method: by means of a new array, and then using two pointers to two arrays, compare the pointer element, placed in a new small array, then move the pointer, the time complexity of O (m + n), Code as follows:

. 1  class Solution {
 2      public  Double findMedianSortedArrays ( int [] nums1, int [] nums2) {
 . 3          // Create a new array, the length of the lengths of the two arrays 
. 4          int [] NUM = new new  int [nums1.length + nums2 .length];
 . 5          // pointer one, a pointer to the array 
. 6          int index1, = 0 ;
 . 7          // pointer two, two point array 
. 8          int index2 = 0 ;
 . 9          for ( int I = 0; I <num.length; I + + ) {
 10              //When two pointers are not the end of the array, compared to the need to move the pointer
 11              // When one end to, directly residual another array into a new array portion 
12 is              IF (index1, <index2 && nums1.length < nums2.length) {
 13 is                  NUM [I] = nums1 [index1,] <= nums2 [index2] nums1 [index1, ++]: nums2 [index2 ++? ];
 14              } the else   IF (index1, < nums1.length) {
 15                  NUM [I] nums1 = [index1, ++ ];
 16              } the else {
 . 17                  NUM [I] = nums2 [index2 ++ ];
 18 is              }           
 . 19          }
 20 is          //Depending on the odd and even array, to calculate the median 
21 is          IF (num.length% 2 == 0 ) {
 22 is              return ( Double ) (NUM [num.length / 21 is] + NUM [num.length / 2]) / ( Double ) 2 ; 
 23 is          } the else {
 24              return NUM [(num.length -. 1) / 2 ];
 25          }
 26 is      }
 27 }

  2) above is that I started to do it, although passed the test, but did not reach the time complexity of O (log (m + n)), but the second method does not yet want to come out, and so later supplemented on, if you have an idea and I can even share exchange, thank you!

Guess you like

Origin www.cnblogs.com/litterCoder/p/11391901.html