Skip to main content

🟢 剑指 Offer II 075. 数组相对排序

LeetCode 提示

题目难度 简单

原题链接 🔗 leetcode

题解1#

/** * @param {number[]} arr1 * @param {number[]} arr2 * @return {number[]} */var relativeSortArray = function(arr1, arr2) {    const posMap = {};    const max2 = arr2.length + 1;    for (let i=0; i <arr2.length; i += 1) {        posMap[arr2[i]] = i+1;    }    arr1.sort((a, b) => (posMap[a] || (a +max2)) - (posMap[b] || (b+max2)));    return arr1;};