Share my c++ solution;Just 25 lines


  • 0
    B
    class Solution {
    public:
        double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
         nums1.insert(nums1.end(),nums2.begin(),nums2.end());
         sort(nums1.begin(),nums1.end());
         double num=0.0;
         if(nums1.size()!=0)
         {
             if(nums1.size()%2==0)
             {
                 num=(nums1[nums1.size()/2]+nums1[(nums1.size()-1)/2])/2.0;
                 return num;
             }
             else
             {
                 num=nums1[nums1.size()/2];
                 return num;
             }
         }
         else
         {
             return false;
         }
        }
    };
    

  • 0
    B
    This post is deleted!

  • 0
    L
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.