class Solution { public List<List<Integer>> combinationSum3(int k, int n) { List<List<Integer>> res = new ArrayList<>(); List<Integer> list = new ArrayList<>(); dfs(res, list, n,k,0,0); return res; }
public void dfs(List<List<Integer>> res, List<Integer> list, int n, int k, int sum, int num) { if(sum > n) return; if (list.size() == k){ if(sum == n) { res.add(new ArrayList<>(list)); } return; } for (int i = num+1; i <= 9; i++) { list.add(i); dfs(res, list, n, k, sum+i, i); list.remove(list.size()-1); }
} }
提交结果
成功 显示详情 执行用时 : 2 ms, 在Combination Sum III的Java提交中击败了67.42% 的用户 内存消耗 : 33.6 MB, 在Combination Sum III的Java提交中击败了29.97% 的用户
近期评论