clueless coding // TODO: be smarter

LeetCode Merge Sorted Array: CPP Solution



TL;DR Code Solution


void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {

    int indexOne = m - 1;
    int indexTwo = n - 1;
    int mainIndex = m + n - 1;

    while (indexOne >= 0 && indexTwo >= 0) {
        if (nums1[indexOne] > nums2[indexTwo]) {
            nums1[mainIndex] = nums1[indexOne];
            indexOne--;
            mainIndex--;
        }
        else {
            nums1[mainIndex] = nums2[indexTwo];
            indexTwo--;
            mainIndex--;
        }
    }

    while (indexTwo >= 0) {
        nums1[mainIndex] = nums2[indexTwo];
        indexTwo--;
        mainIndex--;
    }

}