代码随想录day22 Java版

17.电话号码的字母组合

在套模板的基础上,手动按位置放一个映射表,每次独立处理字符,还要对空字符串单独处理(因为默认生成了StringBuilder是空字符串而不是null)

此处for循环并不像之前从start开始遍历,因为本题每一个数字代表的是不同集合,也就是求不同集合之间的组合,而求组合问题是求同一个集合中的组合

class Solution {
    List<String> res = new ArrayList<>();
    StringBuilder path = new StringBuilder();
    String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    public List<String> letterCombinations(String digits) {
        if (digits.length() == 0) return res;
        backtrack(digits,0);
        return res;
    }
    void backtrack(String digits, int start) {
        if (start == digits.length()) {
            res.add(path.toString());
            return;
        }
        String str = numString[digits.charAt(start) - '0'];
        for (int i = 0; i < str.length(); i++) {
            path.append(str.charAt(i));
            backtrack(digits,start+1);
            path.deleteCharAt(path.length()-1);
        }
    }
}

39. 组合总和

这种复杂算法可以先把给的数组排序方便剪枝

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    int sum = 0;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        backtrack(candidates, target, 0);
        return res;
    }
    void backtrack(int[] candidates, int target, int start) {
        if (sum == target) {
            res.add(new ArrayList<>(path));  
            return;
        }          
        for (int i = start; i < candidates.length; i++) {
            if (sum + candidates[i] > target) break;
            path.add(candidates[i]);
            sum += candidates[i];
            backtrack(candidates, target, i);
            path.removeLast();
            sum -= candidates[i];
        }
    }
}

40.组合总和II

candidates有重复元素,但还不能有重复的组合真的离谱,加一个数组来标记

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    boolean[] used;
    int sum = 0;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        used = new boolean[candidates.length];
        Arrays.fill(used, false);
        Arrays.sort(candidates);
        backtrack(candidates, target, 0);
        return res;
    }
    void backtrack(int[] candidates, int target, int start) {
        if (sum == target) res.add(new ArrayList<>(path));     
        for (int i = start; i < candidates.length; i++) {
            if (sum + candidates[i] > target) break;
            if (i > 0 && candidates[i] == candidates[i - 1] && !used[i - 1]) continue;
            used[i] = true;
            path.add(candidates[i]);
            sum += candidates[i];
            backtrack(candidates, target, i+1);
            used[i] = false;
            path.removeLast();
            sum -= candidates[i];
        }
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值