回溯法

39. 组合总和
https://leetcode-cn.com/problems/combination-sum/

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> list = new ArrayList<>();
        if(candidates == null || candidates.length == 0) return list;
        Arrays.sort(candidates);
        backtrack(list, new ArrayList<>(), candidates, target, 0);
        return list;
    }
    
    public static void backtrack(List<List<Integer>> list, ArrayList<Integer> tmpList, int[] candidates, int remain, int start){
        if(remain < 0){
            return;
        }else if(remain == 0){
            list.add(new ArrayList<>(tmpList));
        }else{
            for(int i = start; i < candidates.length; i++){
                tmpList.add(candidates[i]);
                backtrack(list, tmpList, candidates, remain - candidates[i], i);
                tmpList.remove(tmpList.size() - 1);
            }
        }
    }
}

40. 组合总和 II
https://leetcode-cn.com/problems/combination-sum-ii/

class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> list = new ArrayList<>();
        if(candidates == null || candidates.length == 0){
            return list;
        }
        Arrays.sort(candidates);
        backtrack(list, new ArrayList<>(), candidates, target, 0);
        return list;
    }

    public static void backtrack(List<List<Integer>> list, ArrayList<Integer> tmpList, int[] candidates, int remain, int start){
        if(remain < 0){
            return;
        }else if(remain == 0){
            list.add(new ArrayList<>(tmpList));
        }else{
            for(int i = start; i < candidates.length; i++){
                if(i > start && candidates[i] == candidates[i - 1]) continue;
                tmpList.add(candidates[i]);
                backtrack(list, tmpList, candidates, remain - candidates[i], i + 1);
                tmpList.remove(tmpList.size() - 1);
            }
        }
    }
}

46. 全排列
https://leetcode-cn.com/problems/permutations/

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        if(nums == null || nums.length == 0) return list;
        backtrack(list, new ArrayList<>(), nums);
        return list;
    }

    public static void backtrack(List<List<Integer>> list, ArrayList<Integer> tmpList, int[] nums){
        if(tmpList.size() == nums.length){
            list.add(new ArrayList<>(tmpList));
        }else{
            for(int i = 0; i < nums.length; i++){
                if(tmpList.contains(nums[i])) continue;
                tmpList.add(nums[i]);
                backtrack(list, tmpList, nums);
                tmpList.remove(tmpList.size() - 1);
            }
        }
    }
}

47. 全排列 II
https://leetcode-cn.com/problems/permutations-ii/

class Solution {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        if(nums == null || nums.length == 0) return list;
        Arrays.sort(nums);
        backtrack(list, new ArrayList<>(), nums, new boolean[nums.length]);
        return list;
    }

    public static void backtrack(List<List<Integer>> list, ArrayList<Integer> tmpList, int[] nums, boolean used[]){
        if(tmpList.size() == nums.length){
            list.add(new ArrayList<>(tmpList));
        }else{
            for(int i = 0; i < nums.length; i++){
                if(used[i] || i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) continue;
                tmpList.add(nums[i]);
                used[i] = true;
                backtrack(list, tmpList, nums, used);
                tmpList.remove(tmpList.size() - 1);
                used[i] = false;
            }
        }
    }
}

78. 子集
https://leetcode-cn.com/problems/subsets/

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        if(nums == null) return list;
        Arrays.sort(nums);
        backtrack(list, new ArrayList<>(), nums, 0);
        return list;
    }

    public static void backtrack(List<List<Integer>> list, ArrayList<Integer> tmpList, int[] nums, int start){
        list.add(new ArrayList<>(tmpList));
        for(int i = start; i < nums.length; i++){
            tmpList.add(nums[i]);
            backtrack(list, tmpList, nums, i + 1);
            tmpList.remove(tmpList.size() - 1);
        }
    }
}

90. 子集 II
https://leetcode-cn.com/problems/subsets-ii/

class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        if(nums == null) return list;
        Arrays.sort(nums);
        backtrack(list, new ArrayList<>(), nums, 0);
        return list;
    }

    public static void backtrack(List<List<Integer>> list, ArrayList<Integer> tmpList, int[] nums, int start){
        list.add(new ArrayList<>(tmpList));
        for(int i = start; i < nums.length; i++){
            if(i > start &&  nums[i] == nums[i - 1]) continue;
            tmpList.add(nums[i]);
            backtrack(list, tmpList, nums, i + 1);
            tmpList.remove(tmpList.size() - 1);
        }
    }
}

491. 递增子序列

class Solution {
    public List<List<Integer>> findSubsequences(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        if(nums == null || nums.length == 0) return list;
        backtrack(list, new ArrayList<>(), nums, 0);
        return list;        
    }

    public static void backtrack(List<List<Integer>> list, ArrayList<Integer> tmpList, int[] nums, int index){
        if(index >= nums.length){
            if(tmpList.size() >= 2){
                list.add(new ArrayList<>(tmpList));
            }
            return;
        }
        if(tmpList.isEmpty() || nums[index] >= tmpList.get(tmpList.size() - 1)){
            tmpList.add(nums[index]);
            backtrack(list, tmpList, nums, index + 1);
            tmpList.remove(tmpList.size() - 1);
        }
        if(index > 0 && !tmpList.isEmpty() && nums[index] == tmpList.get(tmpList.size() - 1)){
            return;
        }
        backtrack(list, tmpList, nums, index + 1);
  
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值