2026-04-07 13:05:11
以下是针对每个问题的详细解答:
一、记忆化斐波那契函数答案:
const fibonacci = ((memo = [0, 1]) => { const fib = (n) => { let result = memo[n] if (typeof result !== "number") { result = fib(n - 1) + fib(n - 2) memo[n] = result } return result } return fib})()解析:
答案:
const extractStr = (str) => { const ret = str.match(/:([^:.])*?./g) || [] return ret.map((subStr) => subStr.replace(/[:.]/g, '')) }解析:
答案:
const safeGet = (o, path) => { try { return path.split('.').reduce((o, k) => o[k], o) } catch (e) { return void 666 }}解析:
答案:
const isOverlap = (rect1, rect2) => { const l1 = { x: rect1.x, y: rect1.y } const r1 = { x: rect1.x + rect1.width, y: rect1.y + rect1.height } const l2 = { x: rect2.x, y: rect2.y } const r2 = { x: rect2.x + rect2.width, y: rect2.y + rect2.height } if ( l1.x > r2.x || l2.x > r1.x || l1.y > r2.y || l2.y > r1.y ) return false return true}解析:
答案:
String.prototype.spacify = function () { return this.split('').join(' ')}解析:
答案:
const injectSections = (items, sections) => { const sectionsMap = new Map(sections.map(({ index, content }) => [index, content])) return items.reduce((ret, item, index) => { if (sectionsMap.has(index)) ret.push(sectionsMap.get(index)) ret.push(item) return ret }, [])}解析:
答案:
function *flatten2(arr) { for (let i = 0; i < arr.length; i++) { const item = arr[i] Array.isArray(item) ? yield* flatten2(item) : yield item; }}解析:
答案:
const isSameSet = (s1, s2) => { const isSame = (a, b) => { const values = [...a] for (let val of values) { if (!b.has(val)) return false } return true } return isSame(s1, s2) && isSame(s2, s1)}解析:
答案:
const unique = (arr) => [...new Set(arr)]解析:
答案:
const highlight = (strings, ...args) => { return strings.reduce((str, cur, i) => { return `${str}${cur}${args[i] ? `<span class="highlight">${args[i]}</span>` : '' }` }, '')}解析:
以上解答覆盖了每个问题的核心逻辑和实现细节,确保代码高效且符合题目要求。