Leetcode-median of Sorted Arrays

Source: Internet
Author: User

Median of Sorted Arrays

The problem is to find median, the median number. This means that if the array size is even, it returns the average of the two numbers in the middle, if it is an odd number, which is the middle number.
The algorithm time efficiency requirements are O (log (m + N)), the specific ideas on the Internet are the same.

In addition, now Leetcode C + + arrays have been replaced by vectors, so we have to tidy up the use of vectors.
Reference: http://imatlab.lofter.com/post/286ffc_a81276
Http://www.cnblogs.com/wang7/archive/2012/04/27/2474138.html

C code

#include <iostream>using namespace STD;DoubleFindkth (intA[],intMintB[],intNintK) {if(M > N)returnFindkth (B, N, A, M, K);if(M = =0)returnb[k-1];if(k <=1)returna[0]<b[0]? a[0]:b[0];intia = (k/2< m)? k/2: M, ib = K-ia;if(a[ia-1] < b[ib-1]){returnFindkth (A+ia, M-ia, B, N, K-ia); }Else if(a[ia-1] > b[ib-1]){returnFindkth (A, M, B+ib, N-ib, K-ib); }Else{returna[ia-1]; }}DoubleFindmediansortedarrays (int* Nums1,intNums1size,int* NUMS2,intNums2size) {intK = nums1size + nums2size;if(K &0x1){returnFindkth (Nums1, Nums1size, Nums2, Nums2size, k/2+1); }Else{return(Findkth (Nums1, Nums2size, Nums2, Nums2size, k/2) + findkth (nums1, Nums1size, Nums2, Nums2size, k/2+1)/2); }}intMainintargcChar*argv[]) {intA[] = {1,3,5,7,9};intB[] = {2,4,6,8};Doubleresult = Findmediansortedarrays (A,5B4);printf("%d", (int) result);}

C + + code

Include <iostream>#include <vector>using namespace STD;classSolution { Public:DoubleFindmediansortedarrays ( vector<int>& Nums1, vector<int>& Nums2) {intm = Nums1.size ();intn = nums2.size ();intTotal = m + N;if(Total &0x1)returnFind_kth (Nums1, NUMS2, total/2+1);Else{return(Find_kth (Nums1, NUMS2, total/2) + find_kth (nums1, NUMS2, total/2+1))/2.0; }        }Private:intFind_kth ( vector<int>& A, vector<int>& B,intK) {intm = A.size ();intn = b.size ();if(M > N)returnFind_kth (B, A, K);if(M = =0)returnb[k-1];if(k = =1)returnMin (a[0], b[0]);//min (A, B) and Max (A, b) are available            intia = min (k/2, m), IB = K-ia;if(a[ia-1] < b[ib-1]) {a.erase (A.begin (), A.begin () +ia);returnFind_kth (A, B, K-ia); }Else if(a[ia-1] > b[ib-1]) {b.erase (B.begin (), B.begin () +ib);returnFind_kth (A, B, K-ib); }Else{returna[ia-1]; }        }};intMainintargcChar*argv[]) {intA[] = {1,3,5,7,9};intB[] = {2,4,6,8}; vector<int>A (&a[0], &a[5]);//Copy 5 elements     vector<int>B (&b[0], &b[4]);//Copy 4 elementsSolution solution;Doubleresult = Solution.findmediansortedarrays (A, B);printf("%d", (int) result);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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