回溯相关题型总结

题目汇总

  1. LeetCode_78:子集
  2. LeetCode_90:子集II
  3. LeetCode_46:全排列
  4. LeetCode_47:全排列II
  5. LeetCode_39:组合总和
  6. LeetCode_40:组合总和II
  7. LeetCode_131:分割回文串
  8. LeetCode_77:组合

解题关键点

  1. 这些题的共同之处在于都可用统一的代码模板规范性的求解;
  2. 不同之处在于递归结束条件以及下探到下一层所要传递的参数不同。

目前已知运行最为高效的代码

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        backtrack(list, new ArrayList<Integer>(), nums, 0);
        return list;
    }
    private static void backtrack(List<List<Integer>> list, List<Integer> templist, int[] nums, int start) {
        list.add(new ArrayList<Integer>(templist));
        for (int i = start; i < nums.length; ++i) {
            templist.add(nums[i]);
            backtrack(list, templist, nums, i + 1);
            templist.remove(templist.size() - 1);
        }
    }
}
class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        Arrays.sort(nums);
        backtrack(list, new ArrayList<Integer>(), nums, 0);
        return list;
    }
    private static void backtrack(List<List<Integer>> list, List<Integer> templist, int[] nums, int start) {
        //Terminator
        list.add(new ArrayList<Integer>(templist));
        //Current Logic
        for (int i = start; i < nums.length; ++i) {
            if (i > start && nums[i] == nums[i - 1]) {
                continue;
            }
            templist.add(nums[i]);
            //Drill Down
            backtrack(list, templist, nums, i + 1);
            //Restore Current Data
            templist.remove(templist.size() - 1);
        }
    }
}
class Solution {
    public static List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        backtrack(list, new ArrayList<Integer>(), nums);
        return list;
    }
    private static void backtrack(List<List<Integer>> list, List<Integer> templist, int[] nums) {
        if (templist.size() == nums.length) {
            list.add(new ArrayList<Integer>(templist));
            return;
        } else {
            for (int i = 0; i < nums.length; ++i) {
                if (templist.contains(nums[i])) {
                    continue;
                }
                templist.add(nums[i]);
                backtrack(list, templist, nums);
                templist.remove(templist.size() - 1);
            }
        }
    }
}
class Solution {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        Arrays.sort(nums);
        backtrack(list, new ArrayList<Integer>(), nums, new boolean[nums.length]);
        return list;
    }
    private static void backtrack(List<List<Integer>> list, List<Integer> templist, int[] nums, boolean[] used) {
        if (templist.size() == nums.length) {
            list.add(new ArrayList<Integer>(templist));
            return;
        } else {
            for (int i = 0; i < nums.length; ++i) {
                if (used[i] || i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) {
                    continue;
                }
                used[i] = true;
                templist.add(nums[i]);
                backtrack(list, templist, nums, used);
                used[i] = false;
                templist.remove(templist.size() - 1);
            }
        }

    }
}
class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        backtrack(list, new ArrayList<Integer>(), candidates, target, 0);
        return list;
    }
    private static void backtrack(List<List<Integer>> list, List<Integer> templist, int[] nums, int remain, int start) {
        if (remain < 0) {
            return;
        } else if (remain == 0) {
            list.add(new ArrayList<Integer>(templist));
            return;
        } else {
            for (int i = start; i < nums.length; ++i) {
                templist.add(nums[i]);
                backtrack(list, templist, nums, remain - nums[i], i);
                templist.remove(templist.size() - 1);
            }
        }
    }
}
class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        Arrays.sort(candidates);
        backtrack(list, new ArrayList<Integer>(), candidates, target, 0);
        return list;
    }
    private static void backtrack(List<List<Integer>> list, List<Integer> templist, int[] nums, int remain, int start) {
        if (remain < 0) {
            return;
        } else if (remain == 0) {
            list.add(new ArrayList<Integer>(templist));
            return;
        } else {
            for (int i = start; i < nums.length; ++i) {
                if (i > start && nums[i] == nums[i - 1]) {
                    continue;
                }
                templist.add(nums[i]);
                backtrack(list, templist, nums, remain - nums[i], i + 1);
                templist.remove(templist.size() - 1);
            }
        }
    }
}
class Solution {
    public List<List<String>> partition(String s) {
        List<List<String>> list = new ArrayList<List<String>>();
        backtrack(list, new ArrayList<String>(), s, 0);
        return list;
    }
    private static void backtrack(List<List<String>> list, List<String> templist, String s, int start) {
        int len = s.length();
        if (start == len) {
            list.add(new ArrayList<String>(templist));
            return;
        } else {
            for (int i = start; i < len; ++i) {
                if (isPalindrome(s, start, i)) {
                    templist.add(s.substring(start, i + 1));
                    backtrack(list, templist, s, i + 1);
                    templist.remove(templist.size() - 1);
                }
            }
        }
    }
    private static boolean isPalindrome(String s, int left, int right) {
        while (left < right) {
            if (s.charAt(left++) != s.charAt(right--)) return false;
        }
        return true;
    }
}
class Solution {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        backtrack(n, k, 1, list, new ArrayList<Integer>());
        return list;
    }
    private static void backtrack(int n, int remain, int start, List<List<Integer>> list, List<Integer> templist) {
        if (remain == 0) {
            list.add(new ArrayList<Integer>(templist));
            return;
        } else {
            for (int i = start; i <= n - remain + 1; ++i) {
                templist.add(i);
                backtrack(n, remain - 1, i + 1, list, templist);
                templist.remove(templist.size() - 1);
            }
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值