Simple C++ Solution


  • 0
    I

    class Solution {
    public:

    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
    	vector<int> merged;
    
    	int nums1Index = 0;
    	int nums2Index = 0;
    	int nums1Value = 0;
    	int nums2Value = 0;
    
    	while (nums1Index < m && nums2Index < n)
    	{
    		nums1Value = nums1.at(nums1Index);
    		nums2Value = nums2.at(nums2Index);
    
    		if (nums1Value < nums2Value)
    		{
    			merged.push_back(nums1Value);
    			nums1Index++;
    		}
    		else
    		{
    			merged.push_back(nums2Value);
    			nums2Index++;
    		}
    	}
    
    	while (nums1Index < m)
    	{
    		nums1Value = nums1.at(nums1Index);
    		merged.push_back(nums1Value);
    		nums1Index++;
    	}
    
    	while (nums2Index < n)
    	{
    		nums2Value = nums2.at(nums2Index);
    		merged.push_back(nums2Value);
    		nums2Index++;
    	}
    
    	nums1.swap(merged);
    
    	return;
    }
    

    };


Log in to reply
 

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