4. Median of Sorted Arrays

Source: Internet
Author: User

Difficulty:hard

Here is the 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)).

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
My Submission 1 runtime:240ms
1  Public DoubleFindmediansortedarrays (int[] nums1,int[] nums2) {2list<int> list1=nums1. ToList ();3list<int> list2=Nums2. ToList ();4 List1. AddRange (LIST2);5 List1. Sort ();6         intn = list1. count%2;7         intMid=list1. count/2;8         if(List1. count==0)return 0;9         if(List1. count==1)returnlist1[0];Ten         if(n==1) One         { A             returnList1[mid]; -         } -         Else the         { -             return(Double) (list1[mid-1]+list1[mid])/2; -         } -}
C # Code

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.