- Notifications
You must be signed in to change notification settings - Fork24
Open
Labels
Description
回溯
使用回溯法进行解题,在回溯的常规操作下注意以下两点:
- 每次递归当选满 k 个数时,将其推入最终集合。
- 回溯的过程中为了避免产生重复的组合,需要剪枝,通过指定下次递归的选择范围是
i + 1
来进行剪枝。
constcombine=function(n,k){constres=[]consthelper=function(start,cur){if(cur.length===k){res.push(cur.slice())return}for(leti=start;i<=n;i++){cur.push(i)helper(i+1,cur)cur.pop()}}helper(1,[])returnres}