Share my cpp solution and a little question


  • 0
    Z

    According to my understand, if the input is ([1,3,5,7], 4, [2,4,6], 3) then we should return [1,2,3,4,5,6,7].

    But there is no expected answer of such case. Also my solution have runtime error of this case.

    Is such test case not valid? this question has a restruction that m+n < nums1.size() ?

    Here is my solution that can pass all the test case, but it must under m+n < nums1.size() , or the solution will have runtime error, Is that all the question wants us do?

     class Solution {
    public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        int index=m+n-1;
     
    	while(index>=0){
    		if(m>0&&n>0){
    			nums1[index] = max(nums1[m-1],nums2[n-1]);
    			nums1[m-1]>=nums2[n-1]?m--:n--;
    		}
    		else if(m<=0&&n>0){
    			nums1[index] = nums2[n-1];
    			n--;
    		}
    		else if(m>0&&n<=0){
    			nums1[index] = nums1[m-1];
    			m--;
    		}
    		index--;
    	}
    }
    };

  • 0
    B

    Seems that this runtime error is caused by the judging code


  • 0
    Z

    Thank you @BillyTheShark


Log in to reply
 

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