03 2022 档案
摘要:给定一个不重复的正整数集合,从中取N个数字,使得他们的和为M,写一个函数,求这个N个数字。如有多个,只需要返回一个。 function sumN(arr, n, m, i = 0, result = []) { if (m 0) return result if (i arr.length || n
阅读全文
摘要:k=0或者S.length k返回当前解 // S :数组,需要求组合的集合 // k : 取出元素个数 function combination(S, k) { if (k 0 || S.length k) { return [S.slice(0, k)] } const [first, ...o
阅读全文
摘要:1、最简单全排列,需要0.8秒,concat不改变原数组,返回新数组 function permutation(str, select = []) { if (str.length select.length) { return select.map((item) => str[item]).joi
阅读全文
摘要:决策树的每个决策代表选择一个字符放入子集,决策树的叶子节点代表最终的结果,与顺序无关。 function subsets(str, list) { if (list.length str.length) { return [list.map((v, i) => v ? str[i] : '').jo
阅读全文
摘要:function fn(str) { let obj = {}; let temp = null for (let i = 0; i < str.length; i++) { temp = str[i]; if (obj[temp]) { obj[temp] = obj[temp] + 1; } e
阅读全文
摘要:function restoreIpAddresses(s) { if (s.length > 12) return [] let result = [] fn(s, [], result) return result }; function fn(remain, temp, result) { i
阅读全文
摘要:斐波那契数列由0和1开始,之后的斐波那契数列系数由之前的两数相加,比如:1、1、2、3、5、8、13、21、…… 1、普通递归,有重复计算的问题,递归需要堆栈,内存占用多 function fibonacci(n) { let result = n 1 || n 2 ? 1 : fibonacci(
阅读全文
摘要:function Node(value) { this.value = value; this.left = null; this.right = null; } let a = new Node("a"); let b = a.left = new Node("b"); let c = a.rig
阅读全文
摘要:es6只有findIndex,业务需要从尾部查找,自己实现一个findLastIndex /** * 数组原型添加findLastIndex */ (Array.prototype as any)._jt_findLastIndex = function (callback) { try { if
阅读全文