I am trying to solve the problem of finding the median of two sorted arrays on Leetcode but I am getting the following error: Line 811: Char 16: runtime error: reference binding to null pointer of type 'int' (stl_iterator.h). Can someone help me solve this error?
class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { vector<int>nums3; merge(nums1.begin(),nums2.end(),nums2.begin(),nums2.end(),nums3.begin()); double median; int size_of_merged_vec=nums3.size(); if (size_of_merged_vec%2==0){ median = nums3[size_of_merged_vec/2-1]+ nums3[size_of_merged_vec/2]; } else{ median = nums3[size_of_merged_vec/2-1]; } return median; }
};
Declare num3 as vector<int>nums3(nums1.size() + nums2.size());. Also in your merge second parameter needs to be nums1.end()
num3
vector<int>nums3(nums1.size() + nums2.size());
1.4m articles
1.4m replys
5 comments
57.0k users