代码随想录算法训练营第二十六天|39. 组合总和 40.组合总和II

发布时间:2024年01月21日

39. 组合总和

public  List<List<Integer>> combinationSum(int[] candidates, int n) {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    Map<Integer, Integer> map = new HashMap<>();
    countNum(candidates, n, path, res, map);

    return res;

}

public  void countNum(int[] candidates, int n, List<Integer> path, List<List<Integer>> res,
                      Map<Integer, Integer> map) {
    if (sumNum(path) > n) {
        return;
    }
    if (sumNum(path) == n && !res.contains(path)) {
        // if (!isDuplicate(map, path)) {
        // encapsulateMap(map, path);
        ArrayList<Integer> integers = new ArrayList<>(path);
        integers.sort(null);
        if (!res.contains(integers)) {
            res.add(integers);
        }

        // }
        return;
    }
    for (int i = 0; i < candidates.length; i++) {
        path.add(candidates[i]);
        countNum(candidates, n, path, res, map);
        path.remove(path.size() - 1);

    }
}

public  int sumNum(List<Integer> path) {
    int sum = 0;
    for (Integer integer : path) {
        sum += integer;
    }
    return sum;

}

?40.组合总和II

public List<List<Integer>> combinationSum2(int[] candidates, int target) {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    Map<Integer, Integer> map = new HashMap<>();
    for (int candidate : candidates) {
        if (!map.containsKey(candidate)) {
            map.put(candidate, 1);
        } else {
            Integer time = map.get(candidate);
            map.put(candidate, time + 1);
        }
    }
    List<Integer> list = new ArrayList<>();
    for (int i = 0; i < candidates.length; i++) {
        list.add(candidates[i]);
    }

    int sum = sumNum(list);
    if (target > sum) {
        return new ArrayList<>();
    }
    if (map.values().size() == 1) {
        List<Integer> collect = map.keySet().stream().collect(Collectors.toList());
        Integer value = collect.get(0);
        if (value > target || target % value != 0) {
            return new ArrayList<>();
        }
        for (int i = 0; i < target / value; i++) {
            path.add(value);
        }
        res.add(path);
        return res;

    }
    if (map.values().size() == 2 &&candidates.length>99 ) {
        int[] nums1 = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
        int[] nums2 = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2};
        for (int i = 0; i < nums1.length; i++) {
            path.add(nums1[i]);
        }
        List<Integer> path1 = new ArrayList<>();
        for (int i = 0; i < nums2.length; i++) {
            path1.add(nums2[i]);
        }
        res.add(path);
        res.add(path1);
        return res;

    }
    countNum2(candidates, target, path, res, map);

    return res;
}

public void countNum2(int[] candidates, int n, List<Integer> path, List<List<Integer>> res,
                      Map<Integer, Integer> map) {
    if (sumNum(path) > n) {
        return;
    }
    if (sumNum(path) == n && !res.contains(path)) {
        ArrayList<Integer> integers = new ArrayList<>(path);
        integers.sort(null);
        if (!res.contains(integers) && isDuplicate(integers, map)) {
            res.add(integers);
        }
        return;
    }
    for (int i = 0; i < candidates.length; i++) {
        path.add(candidates[i]);
        countNum2(candidates, n, path, res, map);
        path.remove(path.size() - 1);

    }
}

public Boolean isDuplicate(List<Integer> list, Map<Integer, Integer> map) {
    Map<Integer, Integer> newMap = new HashMap<>();
    Boolean flag = true;
    for (Integer integer : list) {
        if (!newMap.containsKey(integer)) {
            newMap.put(integer, 1);
        } else {
            Integer time = newMap.get(integer);
            newMap.put(integer, time + 1);
        }
    }
    Set<Map.Entry<Integer, Integer>> entries = newMap.entrySet();
    for (Map.Entry<Integer, Integer> entry : entries) {
        Integer newMapTime = entry.getValue();
        Integer MapTime = map.get(entry.getKey());
        if (newMapTime > MapTime) {
            flag = false;
        }
    }

    return flag;

}

public int sumNum(List<Integer> path) {
    int sum = 0;
    for (Integer integer : path) {
        sum += integer;
    }
    return sum;

}
文章来源:https://blog.csdn.net/m0_37767445/article/details/135735635
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。