[leetcode]88. Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
The number of elements initialized in nums1 and nums2 are m and n respectively.
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
Example:
Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
var merge = function (a, m, b, n) {
var i = 0, j = 0, k = 0
var c = [];
var k = 0
//n, m分别为a,b的最后索引; i为a的索引,j为b的索引,k为c的索引
var u = 0;
while (i < m && j < n) {
c[k++] = a[i] < b[j] ? a[i++] : b[j++];
}
var u = 0
while (i < m) {
c.push(a[i++])
}
var u = 0
while (j < n) {
c.push(b[j++])
}
i = 0
var mn = m + n;
a.length = mn;
while (i < mn) {
a[i] = c[i];
i++
}
console.log(a)
};
merge([1, 2, 3, 0, 0, 0], 3, [2, 5, 6], 3)
merge([1], 1, [], 0)
merge( [2,0],1,[1],1)
机器瞎学/数据掩埋/模式混淆/人工智障/深度遗忘/神经掉线/计算机幻觉/专注单身二十五年