... in the js

We see a very simple solution algorithm problem :( use the ...following three points)

Description Title : B to array A and the predetermined two sort where the terminal A is sufficient buffer space to accommodate B. A method for the preparation of the A and B incorporated sort. Initialize the number of elements A and B are m and n.

/**
 * @param {number[]} A
 * @param {number} m
 * @param {number[]} B
 * @param {number} n
 * @return {void} Do not return anything, modify A in-place instead.
 */
var merge = function(A, m, B, n) {
    A.splice(m, A.length - m, ...B.slice(0, n)); //将B插入后A的后面
    A.sort((a, b) => a - b);  //对A、B组合后的数组进行重新排序
};

This solution is really too strong by ...traversing B and obtain all the attributes B, and then covered with the transmission properties of the existing attributes, i.e. by one single extract all attributes and pass them to the new object.

const numbers1 = [1, 2, 3, 4, 5];
const numbers2 = [ ...numbers1, 1, 2, 6,7,8]; // this will be [1, 2, 3, 4, 5, 1, 2, 6, 7, 8]

... three points is referred to this problem the algorithm inside the array / object extensions operator , in addition to being useful as a rest operator (rest operator function parameters received after receiving them can be used to dump an array of real numbers).
eg (rest operator)

function sum(...numbers) {
    return numbers.reduce((accumulator, current) => {
        return accumulator += current
    });
};

sum(1,2) // 3
sum(1,2,3,4,5) // 15

Reference link: http: //www.fly63.com/article/detial/1038
Published 80 original articles · won praise 82 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42893625/article/details/104641891
js