leetcode题目-第四题 两个排序数组的中位数

leetcode-cn.com(领扣),一个不错的代码练习网站

第四题   https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/

打败 81.99 %的选手


题目:

给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 

请找出这两个有序数组的中位数。要求算法的时间复杂度为 O(log (m+n)) 。

你可以假设 nums1 和 nums2 不同时为空。

示例 1:

nums1 = [1, 3]
nums2 = [2]

中位数是 2.0

示例 2:

nums1 = [1, 2]
nums2 = [3, 4]

中位数是 (2 + 3)/2 = 2.5

我的答案:

class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) 
    {
        int ll,i;
        int ll2;
        double n1,n2;
        double a,b;
        int z;
        if ((nums1.size()+nums2.size())%2==1)
            {
                ll = (nums1.size()+nums2.size()+1)/2;
                for(i=1;i<=ll;i++)
                {
                    if(nums1.size()!=0&&nums2.size()!=0)
                    {
                        n1 = nums1[nums1.size()-1];nums1.pop_back();  
                        n2 = nums2[nums2.size()-1];nums2.pop_back();
                        if(n1>n2){nums2.push_back(n2);if(i==ll){return n1;}}
                        if(n2>=n1){nums1.push_back(n1);if(i==ll){return n2;}}
                    }
                    else if(nums1.size()==0&&nums2.size()!=0)
                    {
                        n2 = nums2[nums2.size()-1];nums2.pop_back();
                        if(i==ll){return n2;}
                    }
                    else if(nums2.size()==0&&nums1.size()!=0)
                    {
                        n1 = nums1[nums1.size()-1];nums1.pop_back();
                        if(i==ll){return n1;}
                    }
                    else{return 3.1415926;}
                }
            }

                        if((nums1.size()+nums2.size())%2==0)
                                    {        
                                        ll = (nums1.size()+nums2.size())/2;
                                        ll2 = ll+1;
                                  z=(nums1.size()+nums2.size())-ll2+1;
                                        for(i=1;i<=z;i++)
                                        {
                                            if(nums1.size()!=0&&nums2.size()!=0)
                                            {
                                                n1 = nums1[nums1.size()-1];
                                                n2 = nums2[nums2.size()-1];
                                                if(n1>n2){nums1.pop_back();if(i==z){b=n1;}}
                                                if(n2>=n1){nums2.pop_back();if(i==z){b=n2;}}
                                            }
                                            else if(nums1.size()==0&&nums2.size()!=0)
                                            {
                                                n2 = nums2[nums2.size()-1];nums2.pop_back();
                                                if(i==ll){b=n2;}
                                            }
                                            else if(nums2.size()==0&&nums1.size()!=0)
                                            {
                                                n1 = nums1[nums1.size()-1];nums1.pop_back();
                                                if(i==ll){b=n1;}
                                            }
                                            else{return 3.1415926;} 
                                        }

                                        if(nums1.size()!=0&&nums2.size()!=0)
                                        { 
                                            n1 = nums1[nums1.size()-1];nums1.pop_back();  
                                            n2 = nums2[nums2.size()-1];nums2.pop_back();
                                            if(n1>n2){a=n1;}
                                            if(n1<=n2){a=n2;}
                                        }
                                        else if(nums1.size()==0&&nums2.size()!=0)
                                            {
                                                a = nums2[nums2.size()-1];
                                            }
                                        else if(nums1.size()!=0&&nums2.size()==0)
                                            {
                                                a = nums1[nums1.size()-1];
                                            }
 
                                        return (a+b)/2;

                                 }      
    }
};


打赏

暂无评论

发布评论