每日一题 -- 树
437. 路径总和 III
437. 路径总和 III
var pathSum = function(root, sum) {
let res = 0
const dfs = (root,leave) => {
if(root.val === leave){
res+=1
}
if(!root.left && !root.right){
return
}
if(root.left) dfs(root.left,leave-root.val)
if(root.right) dfs(root.right,leave-root.val)
}
const recuirsion = root => {
if(!root) return
dfs(root,sum)
recuirsion(root.left,sum)
recuirsion(root.right,sum)
}
recuirsion(root)
return res
};
91算法 -- 背包问题
322. 零钱兑换
322. 零钱兑换
分析
var coinChange = function (coins, amount) {
let min = Infinity
coins = coins.sort((a, b) => b - a)
const recursion = (leave, times) => {
if (leave < 0) return
if (leave === 0) {
min = Math.min(min, times)
return
}
if(index < coins.length) return
for(let coin of coins) {
const temp = Math.floor(leave / coins[index])
leave = leave % coins[index]
recursion(leave, times+temp)
recursion
}
console.log(leave, times, index,temp)
}
recursion(amount, 0, 0)
return min === Infinity ? -1 : min
};