88. Merge Sorted Array
https://leetcode.com/problems/merge-sorted-array/description/
Thoughts
Code
/*
* @lc app=leetcode id=88 lang=cpp
*
* [88] Merge Sorted Array
*/
// @lc code=start
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
for (int it = m + n - 1, i = m - 1, j = n - 1; it >= 0; --it) {
const auto a = i >= 0 ? nums1[i] : INT_MIN, b = j >= 0 ? nums2[j] : INT_MIN;
if (a < b) {
nums1[it] = nums2[j--];
} else {
nums1[it] = nums1[i--];
}
}
}
};
// @lc code=end
Analysis
Last updated