LeetCode 第四題答案 C++
阿新 • • 發佈:2019-01-06
class Solution { public: double find_mean(vector<int>&nums){ int me; double result; double er=2; if(nums.size()%2==0){ me=nums.size()/2; result= (nums[me-1]+nums[me])/er; } else{ me=(nums.size()+1)/2; result= nums[me-1]; } return result; } double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { double result; int end=-1; vector<int> execute; vector<int> inser; vector<int> comb; if(!nums1.empty()){ execute=nums1; inser=nums2; } else{ execute=nums2; inser=nums1; } if(inser.empty()){ result=find_mean(execute); } else{ for(int i=0;i<execute.size();i++){ if(i==0){ for(int j=0;j<inser.size();j++){ if(inser[j]<execute[i]) { comb.push_back(inser[j]); end=j; } } } comb.push_back(execute[i]); if(i!=execute.size()-1){ for(int j=0;j<inser.size();j++){ if(inser[j]>=execute[i]&&inser[j]<execute[i+1]) { comb.push_back(inser[j]); end=j; } } } else{ for(int p=end+1;p<inser.size();p++){ comb.push_back(inser[p]); } } } result=find_mean(comb); } return result; } };