Leetcode 4 Median of Sorted Arrays

Source: Internet
Author: User

Problem:there is sorted arraysnums1 andNUMS2of size M and n respectively. Find The median of the sorted arrays. The overall run time complexity should be O (log (M+n)).
Solution:using the method of finding the K-large number, the time complexity is O (log (m+n))Main topic:given two sequential arrays (visual should be in ascending order), and then find the median of two arrays after merging (the median is the same number as the number that is less than the median and greater than the median, if the array is an even number, which is the average of the median two numbers, otherwise the middle number)Problem Solving Ideas: can be found by finding (m+n+1)/2 or finding   (m+n+1)/2 and   (m+n+1)/2+1 obtained, the time complexity is o (log (m+n)) . It can also be sorted by merging arrays, with the optimal complexity being O ((m+n) log (m+n))
Java source code:
public class Solution {public double findmediansortedarrays (int[] nums1, int[] nums2) {if (nums1.length+nums2.        Length) {%2==1) {return findkth (NUMS1,NUMS2, (nums1.length+nums2.length+1)/2); }else{return (1.0*findkth (NUMS1,NUMS2, (nums1.length+nums2.length+1)/2) +findkth (NUMS1,NUMS2, (nums1.length+nums2        . length+1) (/2+1))/2;        }} public int findkth (int[] a,int[] b,int k) {int len1=a.length,len2=b.length;        if (len1==0) return b[k-1];        if (len2==0) return a[k-1];        if (k==1) return min (a[0],b[0]);        int Mid1=min (K/2,LEN1), Mid2=min (K/2,LEN2);            if (A[mid1-1] < b[mid2-1]) {int[] c = new INT[LEN1-MID1];            for (int i=0;i<c.length;i++) C[I]=A[I+MID1];        Return findkth (C,B,K-MID1);            }else{int[] C = new INT[LEN2-MID2];            for (int i=0;i<c.length;i++) C[I]=B[I+MID2];        Return findkth (A,C,K-MID2); }} public int min (int a,int b) {return A&Gt;b?b:a; }}
C Language Source code:
int min (int a, int b) {return a>b?b:a;}    int findkth (int *a,int len1,int* b,int len2,int k) {int mid1,mid2;    if (len1==0) return b[k-1];    if (len2==0) return a[k-1];if (k==1) {return a[0]<b[0]?a[0]:b[0];}    mid1 = min (k/2,len1);    Mid2 = min (k/2,len2);    if (a[mid1-1]<b[mid2-1]) {findkth (A+MID1,LEN1-MID1,B,LEN2,K-MID1);    }else{findkth (A,LEN1,B+MID2,LEN2-MID2,K-MID2);    }}double findmediansortedarrays (int* nums1, int nums1size, int* nums2, int nums2size) {int k=nums1size+nums2size;    if (k%2==1) {return findkth (nums1,nums1size,nums2,nums2size, (nums1size+nums2size+1)/2); }else{return (Findkth (Nums1,nums1size,nums2,nums2size, (nums1size+nums2size+1)/2) + findkth (NUMS1,NUMS1SIZE,NUMS2,    Nums2size, (nums1size+nums2size+1)/2+1) *1.0)/2; }}
C + + source code:
Class Solution {public:double findmediansortedarrays (vector<int>& nums1, vector<int>& nums2) {        int size1=nums1.size (), size2=nums2.size ();        if ((size1+size2)%2) {return findkth (NUMS1,NUMS2, (size1+size2+1)/2);        }else{return (1.0*findkth (NUMS1,NUMS2, (size1+size2+1)/2) +findkth (NUMS1,NUMS2, (size1+size2+1)/2+1))/2;        }}private:int findkth (vector<int> a,vector<int> b,int k) {int len1=a.size ();        int len2=b.size ();        if (len1==0) return b[k-1];        if (len2==0) return a[k-1];        if (k==1) return min (a[0],b[0]);        int mid1 = min (k/2,len1);        int mid2 = min (k/2,len2);            if (A[mid1-1] < b[mid2-1]) {a.erase (A.begin (), A.begin () +mid1);        Return findkth (A,B,K-MID1);            }else{b.erase (B.begin (), B.begin () +mid2);        Return findkth (A,B,K-MID2);    }} int min (int a,int b) {return a>b?b:a; }};
python source code:
Class Solution:    def findkth (self,a,b,k):        Len1=len (a)        Len2=len (b)        if Len1==0:return b[k-1]        if Len2==0:return A[k-1]        if K==1:return min (a[0],b[0])        mid1=min (k/2,len1)        mid2=min (k/2,len2)        if a[ Mid1-1] < b[mid2-1]:            c=a[mid1:len1]            return self.findkth (C,B,K-MID1)        else:            C=b[mid2:len2]            return self.findkth (A,C,K-MID2)    # @param {integer[]} nums1    # @param {integer[]} nums2    # @return { float}    def findmediansortedarrays (self, NUMS1, nums2):        len1=len (NUMS1)        Len2=len (NUMS2)        if ( LEN1+LEN2)%2 = = 1:            return self.findkth (NUMS1,NUMS2, (len1+len2+1)/2)        else:            return (SELF.FINDKTH ( NUMS1,NUMS2, (len1+len2+1)/2) +self.findkth (NUMS1,NUMS2, (len1+len2+1)/2+1))/2.0    def min (self,a,b):        Return a If a<b else b



Leetcode 4 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.