package question;import java.util.LinkedList;import java.util.List;public class Question39 {public static void main(String[] args) {int[] array = new int[]{7,3,2};;new Question39().combinationSum(array, 18).forEach(System.out::println);}List<List<Integer>> res = new LinkedList<>();public List<List<Integer>> combinationSum(int[] candidates, int target) {huiSu(new LinkedList<Integer>(), candidates, target, 0);return res;}public void huiSu(List<Integer> list, int[] candidates, int target, int sum) {if (sum == target) {if (check(list)) res.add(new LinkedList<>(list));return;}if (sum > target) return;for (int i = 0; i < candidates.length; i++) {sum += candidates[i];list.add(candidates[i]);huiSu(list, candidates, target, sum);sum -= candidates[i];list.remove(list.size() - 1);}}public boolean check(List<Integer> list) {for (int i = 0; i < res.size(); i++) {List<Integer> tmp = res.get(i);if (tmp.size() == list.size()) {boolean[] f = new boolean[list.size()];boolean af = true;for (int j = 0; j < tmp.size(); j++) {int a = tmp.get(j);boolean tf = false;for (int k = 0; k < list.size(); k++) {if (!f[k] && list.get(k) == a) {tf = f[k] = true;break;}}if (!tf) {af = false;break;}}if (af) return false;}}return true;}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。