摘要:
回溯算法 const fullPermutationByForEach = (nums = [1, 2, 3]) => { const res = []; const backTrack = (path) => { if (path.length nums.length) { res.push(pa 阅读全文
摘要:
const maxProfit = (arr = [7,1,5,3,4,6]) => { let profit = 0 for(let i = 1; i < arr.length; i++){ let pre = arr[i - 1] let cur = arr[i] if(cur > pre){ 阅读全文
摘要:
/** * 贪心算法 */ const shareCookie = (g = [1,4,5,8], s = [1,3,8]) => { let index = 0 for(let i = 0; i < s.length; i++ ){ if(s[i] >= g[index]){ index++ } 阅读全文
摘要:
/** * 时间复杂度O(2^n) */ const climbStairsByRecursion = (n) => { if(n 1) return 1 if(n 2) return 2 return climbStairsByRecursion(n - 1) + climbStairsByRec 阅读全文
摘要:
/** * 对称树 */ const symmetricTree = { value: 1, left: { value: 2, left: { value: 3, left: { value: 5, }, right: { value: 6, } }, right: { value: 4, lef 阅读全文
摘要:
/** * 相同的树 * 分而治之的思想:判断两树左、右子树值相同,进行递归;若出现不同,则返回false */ const sourceTree = { value: 5, left: { value: 3, left: { value: 2, left: null, right: null }, 阅读全文
摘要:
/** * 注意:left/right值若没有显示设置为null,值即为undefined * 在调用二叉树前、中、后序遍历方法时,由于参数设置了默认值(tree) * 所以进入了死循环 */ const tree = { value: 5, left: { value: 3, left: { va 阅读全文