代码随想录算法训练营第十八天

文档讲解:代码随想录

视频讲解:带你学透回溯算法(理论篇)| 回溯法精讲!_哔哩哔哩_bilibili

状态:

LeetCode 77.组合 

class Solution {
    List<List<Integer>> res;
    List<Integer> path;
    public List<List<Integer>> combine(int n, int k) {
        res = new ArrayList<>();
        path = new ArrayList<>();
        backtracking(1, n, k);
        return res;
    }
    public void backtracking(int startIndex, int n, int k) {
        if(path.size() == k) {
            res.add(new ArrayList(path));
            return;
        }
        for(int i = startIndex; i <= n; i++) {
            path.add(i);
            backtracking(i + 1, n, k);
            path.remove(path.size() - 1);
        }
    }
}
// 剪枝
class Solution {
    List<List<Integer>> res;
    List<Integer> path;
    public List<List<Integer>> combine(int n, int k) {
        res = new ArrayList<>();
        path = new ArrayList<>();
        backtracking(1, n, k);
        return res;
    }
    public void backtracking(int startIndex, int n, int k) {
        if(path.size() == k) {
            res.add(new ArrayList(path));
            return;
        }
        for(int i = startIndex; i <= n - (k - path.size()) + 1; i++) {
            path.add(i);
            backtracking(i + 1, n, k);
            path.remove(path.size() - 1);
        }
    }
}

LeetCode 216.组合总和III
 

class Solution {
    List<List<Integer>> res;
    List<Integer> path;
    public List<List<Integer>> combinationSum3(int k, int n) {
        res = new ArrayList<>();
        path = new ArrayList<>();
        backtracking(k, n, 1);
        return res;
    }
    public void backtracking(int k, int n, int startIndex) {
        if(path.size() == k && n == 0) {
            res.add(new ArrayList(path));
            return;
        }
        if(path.size() == k)return;
        if(n <= 0)return;

        for(int i = startIndex; i <= 9 - (k - path.size()) + 1; i++) {
            path.add(i);
            backtracking(k, n - i, i + 1);
            path.remove(path.size() - 1);
        }
    }
}

LeetCode 17.电话号码的字母组合 

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

心得:都可以套模板,不算太难

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值