88. Merge Sorted Array

C++ | Simplest Way | Brute Force

class Solution {

public:
void merge(vector& nums1, int m, vector& nums2, int n) {

    int i = m-1, j = n-1, index = m+n-1;
    
    while(i>=0 && j>=0){
        if(nums1[i]>nums2[j]) nums1[index--] = nums1[i--];
        else nums1[index--] = nums2[j--];
    }
    
    while(i>=0) nums1[index--] = nums1[i--];
    while(j>=0) nums1[index--] = nums2[j--];
    
}

};

Comments

Popular posts from this blog

1431. Kids With the Greatest Number of Candies

125. Valid Palindrome

771. Jewels and Stones