回溯(组合问题)

77. 组合

题目

给定两个整数 nk,返回范围 [1, n] 中所有可能的 k 个数的组合。

你可以按 任何顺序 返回答案。

示例 1:

输入:n = 4, k = 2
输出:
[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

示例 2:

输入:n = 1, k = 1
输出:[[1]]

答案

class Solution {
    List<List<Integer>> res = new ArrayList();
    List<Integer> list = new LinkedList();
    public List<List<Integer>> combine(int n, int k) {
        deal(n,1,k);
        return res;
    }

    void deal(int n,int startIndex,int end){
        if(list.size()==end){
            res.add(new ArrayList(list));
            return;
        }
        for(int i=startIndex;i<=n;i++){
            list.add(i);
            deal(n,i+1,end);
            list.removeLast();
        }
    }
}






216. 组合总和 III

题目

找出所有相加之和为 nk 个数的组合,且满足下列条件:

  • 只使用数字1到9
  • 每个数字 最多使用一次

返回 所有可能的有效组合的列表 。该列表不能包含相同的组合两次,组合可以以任何顺序返回。

示例 1:

输入: k = 3, n = 7
输出: [[1,2,4]]
解释:
1 + 2 + 4 = 7
没有其他符合的组合了。

示例 2:

输入: k = 3, n = 9
输出: [[1,2,6], [1,3,5], [2,3,4]]
解释:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
没有其他符合的组合了。

示例 3:

输入: k = 4, n = 1
输出: []
解释: 不存在有效的组合。
在[1,9]范围内使用4个不同的数字,我们可以得到的最小和是1+2+3+4 = 10,因为10 > 1,没有有效的组合

答案

class Solution {
    List<List<Integer>> res = new ArrayList();
    List<Integer> list = new LinkedList(); 
    public List<List<Integer>> combinationSum3(int k, int n) {
        int sum = 0;
        deal(9,1,k,n,sum);
        return res;
    }
    void deal(int n,int startIndex,int end,int targetSum,int sum){
        if(sum>targetSum || list.size()>end){
            return;
        }
        if(sum==targetSum && list.size()==end){
            res.add(new ArrayList(list));
            return;
        }
        for(int i=startIndex;i<=n;i++){
            sum += i;
            list.add(i);
            deal(n,i+1,end,targetSum,sum);
            sum -= i;
            list.removeLast();
        }
    }
}






17. 电话号码的字母组合

题目

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

在这里插入图片描述

示例 1:

输入:digits = "23"
输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]

示例 2:

输入:digits = ""
输出:[]

示例 3:

输入:digits = "2"
输出:["a","b","c"]

答案

class Solution {
    List<String> res = new ArrayList();
    StringBuilder sb = new StringBuilder();
    public List<String> letterCombinations(String digits) {
        if(digits.length()==0){
            return res;
        }
        String[] strs = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        deal(digits,0,strs);
        return res;
    }

    void deal(String digits,int startIndex,String[] strs){
        if(startIndex==digits.length()){
            res.add(sb.toString());
            return;
        }

        int index = digits.charAt(startIndex) - '0';
        String str = strs[index];
        for(int i=0;i<str.length();i++){
            sb.append(str.charAt(i));
            deal(digits,startIndex+1,strs);
            sb.deleteCharAt(sb.length()-1);
        }
    }
}






39. 组合总和

题目

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。

candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。

对于给定的输入,保证和为 target 的不同组合数少于 150 个。

示例 1:

输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。

示例 2:

输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]

示例 3:

输入: candidates = [2], target = 1
输出: []

答案

class Solution {
    List<List<Integer>> res = new ArrayList();
    List<Integer> list = new LinkedList();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        deal(candidates,0,target,0);
        return res;
    }

    void deal(int[] candidates,int startIndex,int targetSum,int sum){
        if(sum>targetSum){
            return;
        }
        if(sum==targetSum){
            res.add(new ArrayList(list));
        }
        for(int i=startIndex;i<candidates.length;i++){
            sum += candidates[i];
            list.add(candidates[i]);
            deal(candidates,i,targetSum,sum);
            sum -= candidates[i];
            list.removeLast();
        }
    }
}






40. 组合总和 II

题目

给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用 一次

**注意:**解集不能包含重复的组合。

示例 1:

输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]

示例 2:

输入: candidates = [2,5,2,1,2], target = 5,
输出:
[
[1,2,2],
[5]
]

答案

class Solution {
    List<List<Integer>> res = new ArrayList();
    List<Integer> list = new LinkedList();
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        deal(candidates,0,target,0);
        return res;
    }

    void deal(int[] candidates,int startIndex,int targetSum,int sum){
        if(sum>targetSum){
            return;
        }
        if(sum==targetSum){
            res.add(new ArrayList(list));
            return;
        }
        for(int i=startIndex;i<candidates.length;i++){
            if(i>startIndex && candidates[i]==candidates[i-1]){//排序加去重
                continue;
            }
            sum += candidates[i];
            list.add(candidates[i]);
            deal(candidates,i+1,targetSum,sum);
            sum -= candidates[i];
            list.removeLast();
        }
    }
}






131. 分割回文串

题目

给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。

回文串 是正着读和反着读都一样的字符串。

示例 1:

输入:s = "aab"
输出:[["a","a","b"],["aa","b"]]

示例 2:

输入:s = "a"
输出:[["a"]]

答案

class Solution {
    List<List<String>> res = new ArrayList();
    List<String> list = new LinkedList();
    public List<List<String>> partition(String s) {
        deal(s,0);
        return res;
    }

    void deal(String s,int startIndex){
        if(startIndex==s.length()){
            res.add(new ArrayList(list));
            return;
        }
        for(int i=startIndex;i<s.length();i++){
            if(flag(s,startIndex,i)){
                list.add(s.substring(startIndex,i+1));
            }else{
                continue;
            }
            deal(s,i+1);
            list.removeLast();
        }
    }
    boolean flag(String s,int start,int end){
        for(;start<end && end<s.length();start++,end--){
            if(s.charAt(start)!=s.charAt(end)){
                return false;
            }
        }
        return true;
    }
}






93. 复原 IP 地址

题目

有效 IP 地址 正好由四个整数(每个整数位于 0255 之间组成,且不能含有前导 0),整数之间用 '.' 分隔。

  • 例如:"0.1.2.201" "192.168.1.1"有效 IP 地址,但是 "0.011.255.245""192.168.1.312""192.168@1.1"无效 IP 地址。

给定一个只包含数字的字符串 s ,用以表示一个 IP 地址,返回所有可能的有效 IP 地址,这些地址可以通过在 s 中插入 '.' 来形成。你 不能 重新排序或删除 s 中的任何数字。你可以按 任何 顺序返回答案。

示例 1:

输入:s = "25525511135"
输出:["255.255.11.135","255.255.111.35"]

示例 2:

输入:s = "0000"
输出:["0.0.0.0"]

示例 3:

输入:s = "101023"
输出:["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]

答案

class Solution {
    List<String> res = new ArrayList();
    List<String> list = new LinkedList();
    public List<String> restoreIpAddresses(String s) {
        deal(s,0,0);
        return res;
    }

    void deal(String s,int startIndex,int count){
        if(count>4){
            return;
        }
        if(count==4 && startIndex==s.length()){
            res.add(String.join(".",list));
            return;
        }
        for(int i=startIndex;i<s.length() && i<startIndex+3;i++){
            if(s.charAt(startIndex)=='0' && i>startIndex) return;//以0开头只截取0,后面不截取
            int val = Integer.parseInt(s.substring(startIndex,i+1));
            if(val>=0 && val<=255){
                list.add(val+"");
                deal(s,i+1,count+1);
                list.removeLast();
            }
        }
    }
}
  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值