77. Combinations

灰太狼 2021-12-22 06:51 326阅读 0赞
  1. class Solution {
  2. public List<List<Integer>> combine(int n, int k) {
  3. List<List<Integer>> res=new ArrayList<List<Integer>>();
  4. combine(n, k, new ArrayList<Integer>(), res);
  5. return res;
  6. }
  7. private void combine(int num, int k, List<Integer> list, List<List<Integer>> res){
  8. if(k==0)
  9. {
  10. res.add(new ArrayList<Integer>(list));
  11. return;
  12. }
  13. if(num==0)
  14. return;
  15. combine(num-1,k,list,res);
  16. list.add(num);
  17. combine(num-1,k-1,list,res);
  18. list.remove(list.size()-1);
  19. }
  20. }

转载于:https://www.cnblogs.com/asuran/p/7599822.html

发表评论

表情:
评论列表 (有 0 条评论,326人围观)

还没有评论,来说两句吧...

相关阅读

    相关 77. 组合

    > 保持忙碌。——《人性的优点》 77. 组合 给定两个整数 n 和 k,返回范围 \[1, n\] 中所有可能的 k 个数的组合。 你可以按 任何顺序 返回答案。