Simple Solution with no extra space


  • 0
    N
     void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        
        for (int i = m; i < nums1.size(); i++)
        {
            nums1.erase(nums1.begin() + i);
            i--;
        }
        
        for (int i = 0; i < n; i++)
        {
            nums1.push_back(nums2[i]);
        }
        
            
        sort(nums1.begin(), nums1.end());

  • 0
    L

    But the arrays are already sorted, what a waste to sort them again!


Log in to reply
 

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