简体   繁体   中英

leetcode 88. Merge Sorted Array c++ runtime error

I am doing leetcode problem 88. Here is the link: Merge Sorted Array .

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.

I use c++ to solve this problem. But I always got runtime error like this:

Runtime Error Message:
Line 922: Char 34: runtime error: reference binding to null pointer of type 'value_type' (stl_vector.h)
Last executed input:
[1]
1
[]
0

Here is my code:

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        int output[m+n] = {0};
        int i = 0;
        int j = 0;
        if(sizeof(nums1) == 0)
        {
            for(i = 0; i < n; i++)
            {
                nums1[i] = nums2[i];
            }
            return;
        }
        if(sizeof(nums2) == 0)
        {
            return;
        }
        for(int k = 0; k < m+n; k++)
        {
            if(nums1[i] <= nums2[j])
            {
                if(i < m)
                {
                    output[k] = nums1[i];
                    i++; 
                }
                else if(j < n)
                {
                    output[k] = nums2[j];
                    j++;
                }
            }
            else if(nums1[i] >= nums2[j])
            {
                if(j < n)
                {
                    output[k] = nums2[j];
                    j++;
                }
                else if(i < m)
                {
                    output[k] = nums1[i];
                    i++;
                }
            }
        }
        for(i = 0; i < m+n; i++)
        {
            nums1[i] = output[i];
        }
    }
};

It's easy to test, just click the link above and copy my code then submit it. I hope someone could help me.

When using std::vector , to check if it's empty the member function size() should be used. sizeof is not the right check.

  if(sizeof(nums1) == 0)

should be

nums1.size()==0

Same goes for nums2 vector as well. Another thing to note is the output array size is unknown at compile time and VLA is not typically supported, instead use vector here as well

std::vector<int> output(m+n,0); //Initialized with m+n elements with value 0

There are some flaws in your current approach, like:

  • sizeof(nums1) operator will only give you 0 in case: nums1[],m=0,nums2[],n=0 , because for all other cases when m=0 but n!=0 , nums1 will contain 0 's and it won't be an empty list. Ex. nums1[0,0],m=0,nums2[1,2],n=2 . To handle this, check if m==0 or n==0 instead of using sizeof .

  • In line if(nums1[i] <= nums2[j]) , if j>=n , it will throw a segmentation error.

  • You may find more flaws if you check all test cases on LeetCode.

A correct approach is given below. This solution is accepted on LeetCode. I've added comments which are mostly self-explanatory. Feel free to comment if you have any queries.

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        int output[m+n] = {0};
        int i = 0;
        int j = 0;
        int k = 0;
        if(m == 0) // nums1 is empty. Ex. nums1[0,0],m=0 and nums2[1,2],n=2
        {
            for(i = 0; i < n; i++)
            {
                nums1[i] = nums2[i];
            }
            return;
        }
        if(n == 0) // nums2 is empty. Ex. nums1[1,2],m=2 and nums2[],n=0
        {
            return;
        }
        for(k = 0; k < m+n; k++)
        {
            if(i>=m || j>=n) // Check if we've exhausted any of nums1 or nums2 list
                break;
            if(nums1[i] <= nums2[j]) // if nums1[i] <= nums2[j]
            {
                output[k] = nums1[i];
                i++;
            }
            else{ // else, nums1[i] > nums2[j]
                output[k] = nums2[j];
                j++;
            }
        }
        while(i<m){ // If some elements are left in nums1. Ex: nums1[4,0,0],m=1 and nums2[1,2],n=2
            output[k] = nums1[i];
            i++;
            k++;
        }
        while(j<n){ // If some elements are left in nums2. Ex: nums1[1,2,0],m=2 and nums2[4],n=1
            output[k] = nums2[j];
            j++;
            k++;
        }
        for(i = 0; i < m+n; i++) // Copy back to nums1
        {
            nums1[i] = output[i];
        }
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM