Skip to content

Latest commit

 

History

History
34 lines (30 loc) · 754 Bytes

1640. 能否连接形成数组.md

File metadata and controls

34 lines (30 loc) · 754 Bytes
/**
 * @param {number[]} arr
 * @param {number[][]} pieces
 * @return {boolean}
 */
var canFormArray = function (arr, pieces) {
    const map = new Map(),
        { length } = arr;

    for (const piece of pieces) {
        map.set(piece[0], piece);
    }

    let i = 0;

    while (i < length) {
        if (!map.has(arr[i])) {
            return false;
        }
        const compareArray = map.get(arr[i]);
        let j = 0;
        while (j < compareArray.length) {
            if (arr[i] !== compareArray[j]) {
                return false;
            }
            ++i, ++j;
        }
    }
    return true;
};