lynhao / day-by-day

每日更新一道算法题
MIT License
0 stars 0 forks source link

组合(draf。。。) #17

Open lynhao opened 3 years ago

lynhao commented 3 years ago

var combine = function(n, k, m) { function generation(n, k, start, s) { if (s.length === k) { let sum = 0 for(let i = 0; i < k; i++) { sum += s[i] } if (sum === m) { res.push(s.slice()) } return } for(let i = start; i < n.length; i++) { s.push(n[i]) generation(n, k, i + 1, s) console.log(s, res) s.pop() } return } let res = [] let s = [] if (n.length <= 0 || k > n.length || k <= 0) { return res } generation(n, k, 0, s) return res };