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

文章讲述了如何使用Java编程实现两个问题的解决方案:组合总和系列函数(组合所有数字加和等于目标值),以及电话号码的字母组合生成。主要涉及递归和回溯算法的应用。
摘要由CSDN通过智能技术生成

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;

}
17.电话号码的字母组合 
public List<String> letterCombinations(String digits) {
    if ("".equals(digits)) {
        return new ArrayList<>();
    }
    List<String> path = new ArrayList<>();
    StringBuffer buffer = new StringBuffer();
    char[] chars = digits.toCharArray();
    dfs3(chars, 0, buffer, path);
    return path;
}

final String[] A = {
        "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"
};

public void dfs3(char[] nums, int index, StringBuffer stringBuffer, List<String> path) {
    if (index == nums.length) {
        String newString = stringBuffer.toString();
        path.add(newString.toString());
        return;
    }
    if (index > nums.length) {
        return;
    }
    int num = nums[index] - '0';
    char[] chars = A[num].toCharArray();
    for (int i = 0; i < chars.length; i++) {
        stringBuffer.append(String.valueOf(chars[i]));
        dfs3(nums, index + 1, stringBuffer, path);
        stringBuffer.deleteCharAt(stringBuffer.length() - 1);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值