力扣刷题记录-回溯专题

回溯专题

77. 组合

一、题目描述

二、解题思路

回溯法

三、AC代码

class Solution {
    
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    
    public List<List<Integer>> combine(int n, int k) {
        backTrack(n, k, 1);
        return res;
    }
    
    private void backTrack(int n, int k, int startIndex) {
        if (path.size() == k) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex; i <= n-(k-path.size())+1; i++) {
            path.add(i);
            backTrack(n, k, i+1);
            path.removeLast();
        }
    }
}

39.组合总和

一、题目描述

二、解题思路

回溯法

三、AC代码

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

123.组合总和II

一、题目描述

二、解题思路

回溯法

三、AC代码

class Solution {

    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    boolean[] used;

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        used = new boolean[candidates.length];
        backTrack(candidates, 0, target, 0);
        return res;
    }

    private void backTrack(int[] candidates, int sum, int target, int idx) {
        if (sum == target) {
            res.add(new ArrayList(path));
            return;
        }
        for (int i = idx; 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]);
            backTrack(candidates, sum+candidates[i], target, i+1);
            path.removeLast();
            used[i] = false;
        }
    }

}

216.组合总和 III

一、题目描述

二、解题思路

回溯法

三、AC代码

class Solution {

    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();

    public List<List<Integer>> combinationSum3(int k, int n) {
        backTrack(n, k, 1, 0);
        return res;
    }

    private void backTrack(int targetSum, int k, int startIndex, int sum) {
		if (path.size() == k) {
			if (sum == targetSum) {
                res.add(new ArrayList<>(path));
            }
			return;
		}
		for (int i = startIndex; i <= 9 - (k - path.size()) + 1; i++) {
            if (sum + i > targetSum) {
                break;
            }
			path.add(i);
			backTrack(targetSum, k, i + 1, sum+i);
			path.removeLast();
		}
	}

}

46. 全排列

一、题目描述

二、解题思路

回溯法

三、AC代码

class Solution {

    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    boolean[] used;

    public List<List<Integer>> permute(int[] nums) {
        used = new boolean[nums.length];
        backTrack(nums);
        return res;
    }

    private void backTrack(int[] nums){
        if (path.size() == nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++){
            if (used[i]){
                continue;
            }
            used[i] = true;
            path.add(nums[i]);
            backTrack(nums);
            path.removeLast();
            used[i] = false;
        }
    }

}
控制台

47.全排列 II

一、题目描述

二、解题思路

回溯法

三、AC代码

class Solution {

    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    boolean[] used;

    public List<List<Integer>> permuteUnique(int[] nums) {
        used = new boolean[nums.length];
        Arrays.sort(nums);
        backTrack(nums);
        return res;
    }

    private void backTrack(int[] nums) {
        if (path.size() == nums.length) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) {
                continue;
            }
            if (used[i]) {
                continue;
            }
            used[i] = true;
            path.add(nums[i]);
            backTrack(nums);
            path.removeLast();
            used[i] = false;
        }
    }

}

78.子集

一、题目描述

二、解题思路

回溯法

三、AC代码

class Solution {

    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    
    public List<List<Integer>> subsets(int[] nums) {
        backtrack(nums, 0);
        return res;
    }

    private void backtrack(int[] nums, int startIndex){
        res.add(new ArrayList<>(path));
        if (startIndex >= nums.length){ 
            return;
        }
        for (int i = startIndex; i < nums.length; i++){
            path.add(nums[i]);
            backtrack(nums, i + 1);
            path.removeLast();
        }
    }

}

90.子集II

一、题目描述

二、解题思路

回溯法

三、AC代码

class Solution {

    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    boolean[] used;

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        used = new boolean[nums.length];
        backTrack(nums, 0);
        return res;
    }
    
    private void backTrack(int[] nums, int startIndex){
        res.add(new ArrayList<>(path));
        if (startIndex >= nums.length){
            return;
        }
        for (int i = startIndex; i < nums.length; i++){
            if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]){
                continue;
            }
            path.add(nums[i]);
            used[i] = true;
            backTrack(nums, i + 1);
            path.removeLast();
            used[i] = false;
        }
    }
    
}

17.电话号码的字母组合

一、题目描述

二、解题思路

回溯法

三、AC代码

class Solution {

    List<String> res = new ArrayList<>();
    StringBuilder tmp = new StringBuilder();

    public List<String> letterCombinations(String digits) {
        String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        backTrack(digits, numString, 0);
        return res;
    }

    public void backTrack(String digits, String[] numString, int num) {
        if (num == digits.length()) {
            if (digits.length() != 0) {
                res.add(tmp.toString());
            }
            return;
        }
        String str = numString[digits.charAt(num) - '0'];
        for (int i = 0; i < str.length(); i++) {
            tmp.append(str.charAt(i));
            backTrack(digits, numString, num+1);
            tmp.deleteCharAt(tmp.length()-1);
        }
    }
    
}

131.分割回文串

一、题目描述

二、解题思路

回溯法

三、AC代码


参考代码随想录:https://programmercarl.com/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值