组合总和3
解题思路:递归回溯
class Solution { public List<List<Integer>> result = new ArrayList<List<Integer>>(); public List<List<Integer>> combinationSum3(int k, int n) { List<Integer> list = new ArrayList<>(); combinationSum3(1,k,n,list); return result; } public void combinationSum3(int start, int k, int n, List<Integer> list) { if(k==0) { if(n==0) { List<Integer> newList = (List)((ArrayList)list).clone(); result.add(newList); } return; } for(int i=start;i<10;++i) { list.add(i); combinationSum3(i+1,k-1,n-i,list); list.remove((Integer)i); } } }
原文地址:https://www.cnblogs.com/erdanyang/p/11505001.html
时间: 2024-10-02 19:42:43