leetcode_c++:Median_of _two_sorted_arrays(004)

题目

两个已经排序好的数组num1和num2,长度分别是m和n。找到两个有序数组的中位数(中间的那个数,若为偶数,则是中间的两个数的平均数)要求复杂度是O(lg(n+m))

  • 算法1
    Merge两个数组,然后取得中位数

复杂度:O(n+m)

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

const int maxn=100;

class Solution{
public:
    double findMedianSortedArrays(int A[],int m,int B[],int n){
        vector<int> C;
        int pa=0,pb=0; //pointer of A[] & B[]

        while (pa<m || pb<n) {
            if(pa==m){
                C.push_back(B[pb++]);
                continue;
            }
            if(pb==n){
                C.push_back(A[pa++]);
                continue;
            }
            if(A[pa]>B[pb])
                C.push_back(B[pb++]);
            else
                C.push_back(A[pa++]);
        }
        if((n+m)&1)  // 奇数,按位与
            return C[(n+m)/2];
        else
            return (C[(n+m)/2-1]+C[(n+m)/2])/2.0;

    }
};

int main()
{
    int n,m;
    int A[maxn],B[maxn];
    Solution s;
    while(cin>>n){
        for(int i=0;i<n;i++)
            cin>>A[i];
        cin>>m;
        for(int i=0;i<m;i++)
            cin>>B[i];
        cout<<s.findMedianSortedArrays(A,n, B, m)<<endl;
    }

    return 0;
}
  • 算法2
    求两个数组中的第k大的数
    若A[k/2-1]
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 0;

class Solution {
    private:
        double findKthSortedArrays(int A[], int m, int B[], int n, int k) {
            if (m < n) {
                swap(n, m);
                swap(A, B);
            }
            if (n == 0)
                return A[k - 1];
            if (k == 1)
                return min(A[0], B[0]);

            int pb = min(k / 2, n), pa = k - pb;
            if (A[pa - 1] > B[pb - 1])
                return findKthSortedArrays(A, m, B + pb, n - pb, k - pb);
            else if (A[pa - 1] < B[pb - 1])
                return findKthSortedArrays(A + pa, m - pa, B, n, k - pa);
            else
                return A[pa - 1];
        }

    public:
        double findMedianSortedArrays(int A[], int m, int B[], int n) {
            if ((n + m)&1)
                return findKthSortedArrays(A, m, B, n, (n + m) / 2 + 1);
            else
                return (findKthSortedArrays(A, m, B, n, (n + m) / 2 + 1) +
                        findKthSortedArrays(A, m, B, n, (n + m) / 2)) / 2.0;
        }
};

int main() {
    int n, m;
    int A[100], B[100];
    Solution s;
    while (cin >> n) {
        for (int i = 0; i < n; i++) 
            cin >> A[i];
        cin >> m;
        for (int i = 0; i < m; i++) 
            cin >> B[i];
        cout << s.findMedianSortedArrays(A, n, B, m) << endl;
    }
    return 0;
}

  • vector版本

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;


const int N = 0;

class Solution {  
public:  
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {  
        int m = nums1.size();  
        int n = nums2.size();  
        int value1 = KthValue(nums1, m, nums1.begin(), nums2, n, nums2.begin(), (m+n+1)/2);  
        double result = value1;  
        if (((m+n) & 1) == 0)  
        {  
            int value2 = KthValue(nums1, m, nums1.begin(), nums2, n, nums2.begin(), (m+n)/2+1);  
            result = ((double)value1 + (double)value2)/2;  
        }  

        return result;   
    }  

private:  
    int KthValue(vector<int> &nums1, int size1, vector<int>::iterator it1, vector<int> &nums2,   
                int size2, vector<int>::iterator it2, int k)  
    {  
        /*if (size1 > size2) { return KthValue(nums2, size2, it2, nums1, size1, it1, k); } */

        if(size1>size2)
        {
            swap(size1,size2);
            nums1.swap(nums2);
        }

        if (size1 == 0)  
        {  
            return *(it2+k-1);  
        }  

        if (k == 1)  
        {  
            return min(*it1, *it2);  
        }  

        int offset1 = min(k/2, size1);  
        int offset2 = k - offset1;  
        if (*(it1+offset1-1) <= *(it2+offset2-1))  
        {  
            return KthValue(nums1, size1-offset1, it1+offset1, nums2, offset2, it2, k-offset1);  
        }  
        else  
        {  
            return KthValue(nums1, offset1, it1, nums2, size2-offset2, it2+offset2, k-offset2);  
        }  
    }  
}; 

int main() {
    int n, m,t;
   vector<int> A;
   vector<int> B;
    Solution s;
  while(cin>>n)
  {
      for(int i=0;i<n;i++)
      {
          cin>>t;
          A.push_back(t);
      }
      cin>>m;
      for(int i=0;i<m;i++)
      {
          cin>>t;
          B.push_back(t);
      }

      for(int i=0;i<n;i++)
          cout<<"A= "<<A[i]<<endl;

      for(int i=0;i<m;i++)
          cout<<"B= "<<B[i]<<endl;

      cout<<s.findMedianSortedArrays(A,B)<<endl;
  }
    return 0;
}

你可能感兴趣的:(leetcode_c++:Median_of _two_sorted_arrays(004))