java机试题*:组合总和
组合总和
给定一个无重复元素的正整数数组 candidates 和一个正整数 target ,找出 candidates 中所有可以使数字和为目标数 target 的唯一组合。
candidates 中的数字可以无限制重复被选取。如果至少一个所选数字数量不同,则两种组合是唯一的。
对于给定的输入,保证和为 target 的唯一组合数少于 150 个。
示例 1:
输入: candidates = [2,3,6,7], target = 7
输出: [[7],[2,2,3]]
示例 2:
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:
class Solution { List<List<Integer>> res = new ArrayList<>(); //记录答案 List<Integer> path = new ArrayList<>(); //记录路径 public List<List<Integer>> combinationSum(int[] candidates, int target) { dfs(candidates,0, target); return res; } public void dfs(int[] c, int u, int target) { if(target < 0) return ; if(target == 0) { res.add(new ArrayList(path)); return ; } for(int i = u; i < c.length; i++){ if( c[i] <= target) { path.add(c[i]); dfs(c,i,target - c[i]); // 因为可以重复使用,所以还是i path.remove(path.size()-1); //回溯,恢复现场,走到这里说明没有满足和,回溯 } } } } 作者:lin-shen-shi-jian-lu-k 链接:https://leetcode-cn.com/problems/combination-sum/solution/hui-su-suan-fa-zui-tong-su-yi-dong-de-da-6qyl/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
浙公网安备 33010602011771号