Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
1.0 kB
5
Indexable
class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
           
         //Beats 100.00%

         cin.tie(nullptr)->sync_with_stdio(0);

        int i = 0, j = 0, index = 0;
        vector<int> tempArray(n + m);
        while (i < m || j < n) {

            while (i < m && j < n && nums1[i] <= nums2[j]) {
                tempArray[index] = nums1[i];
                i += 1;
                index += 1;
            }

            while (i < m && j < n && nums2[j] <= nums1[i]) {
                tempArray[index] = nums2[j];
                j += 1;
                index += 1;
            }
            while (i >= m && j < n) {
                tempArray[index] = nums2[j];
                j += 1;
                index += 1;
            }
            while (j >= n && i < m) {
                tempArray[index] = nums1[i];
                i += 1;
                index += 1;
            }
        }
        nums1 = tempArray;
    }
};
Leave a Comment