【力扣刷题】Day23——回溯专题


上一篇文章:【力扣刷题】Day22——回溯专题_塔塔开!!!的博客-CSDN博客

二、分割问题

7. 分割回文串

题目链接:131. 分割回文串 - 力扣(LeetCode)

枚举方案类的题目都可以用回溯法来实现,枚举该串的所有子串,判断子串是否是回文串即可!

Code

class Solution {
    
    
    List<List<String>> res = new ArrayList<>();
    List<String> path = new ArrayList<>();
    int n;
    public List<List<String>> partition(String s) {
    
    
        n = s.length();
        dfs(s, 0);
        return res;
    }
    public void dfs(String s, int u){
    
    
        if(u == n){
    
    
            res.add(new ArrayList(path));
            return ;
        }
        // 从u开始枚举
        for(int i = u; i < n; i ++){
    
    
            if(is_huiwen(s.substring(u, i + 1))){
    
    
                path.add(s.substring(u, i + 1));
                dfs(s, i + 1);
                path.remove(path.size() - 1);
            }
        }
    }
    /**
        判断子串是否为回文串
     */
    public boolean is_huiwen(String s){
    
    
        char[] cs = s.toCharArray();
        int l = 0, r = cs.length - 1;
        while(l < r){
    
    
            if(cs[l] != cs[r]){
    
    
                return false;
            }else {
    
    
                l ++;
                r --;
            }
        }
        return true;
    }
}

8. 复原IP地址

题目链接:93. 复原 IP 地址 - 力扣(LeetCode)

思路:IP地址由4个分段组成,每一个分段的范围在0~255,每一段都有三次机会,选1/2/3个,我们可以通过枚举每一段的数字是否符合IP的规定,判断能否构成IP地址或者是否合理。

值得注意的是,数字前不能有前置0,当枚举的时候这一段的第一个字符即为0,那么本段就只能为0了!

Code

class Solution {
    
    
    List<String> res = new ArrayList<>();
    StringBuilder path = new StringBuilder();// 可能的一个ip
    public List<String> restoreIpAddresses(String s) {
    
    
        dfs(s, 0);// 0表示从第一段开始枚举
        return res;
    }
    public void dfs(String s, int u){
    
    
        if(u == 4 && s.length() == 0){
    
    
            res.add(path.toString());
            return ;
        }
        // 剪枝
        if(u > 4){
    
    
            return ;
        }

        // 枚举每一种可能
        for(int i = 0; i < s.length() && i < 3; i ++){
    
    // 每一段最多为3个数
            // 如果当前字符串的第一个字符为0,由于数字前置非0,那么本次ip地址的这一段只能为0,直接break本段
            if(i != 0 && s.charAt(0) == '0'){
    
    // 不是第一个字符 且为0
                break;
            }
            // 找到一个片段就进一步判断其合法性(每一段1/2/3个数)
            String tmp = s.substring(0, i + 1); // 
            if(Integer.valueOf(tmp) <= 255){
    
    
                if(path.length() != 0){
    
    
                    tmp = "." + tmp;
                }
                path.append(tmp);
                dfs(s.substring(i + 1), u + 1);// 枚举下一个
                path.delete(path.length() - tmp.length(), path.length());// 回溯
            }
            
        }
    }
}

三、子集问题

9. 子集I

题目链接:78. 子集 - 力扣(LeetCode)

思路一:DFS枚举指数类型(每一个数两种选择:选或者不选)

Code

class Solution {
    
    
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    int n;
    public List<List<Integer>> subsets(int[] nums) {
    
    
        n = nums.length;
        dfs(nums, 0);
        return res;
    }
    public void dfs(int[] nums, int u){
    
    
        if(u == n){
    
    
            res.add(new ArrayList(path));
            return ;
        }
        // 每一个数选或者不选

        // 选
        path.add(nums[u]);
        dfs(nums, u + 1);
        path.remove(path.size() - 1);

        // 不选
        dfs(nums, u + 1);
    }
}

思路二:二进制枚举

由于每一个数只有两种选择,且题目明确每一个数互不相同,我们利用二进制枚举(0表示不选,1表示选),从0 ~ 2^n - 1,总共2^n种可能:

[1, 2, 3]:

000 []

001 [1]

010 [2]

011 [1, 2]

100 [3]

101 [1, 3]

Code

class Solution {
    
    
    public List<List<Integer>> subsets(int[] nums) {
    
    
        int n = nums.length;
        List<List<Integer>> res = new ArrayList<>();
        
        for(int i = 0; i < (1 << n); i ++){
    
    
            List<Integer> path = new ArrayList<>();
            for(int j = 0; j < n; j ++){
    
    
                if((i >> j & 1) == 1){
    
    
                    path.add(nums[j]);
                }
            }
            res.add(new ArrayList(path));
        }
        return res;
    }
}

10.子集II

题目链接:90. 子集 II - 力扣(LeetCode)

本题与上一题的区别是,本题数组中可能包含重复元素,解集 不能 包含重复的子集,为此要进行去重操作。

[1,2,2]
输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]

如何去重,即保证每一个元素只能被选择一次,这题跟组合总和III很相像,都是排序去重,不同的是本题求的是子集,组合2求的是和满足条件的组合,比如重复的一段 2 2 2 2 2 :

2是子集

2 2也是子集

2 2 2也是子集

因此如果我们希望去重的话,不能单纯的利用「某个下标是否被选择」来进行决策,而是要找到某个数值的连续一段,根据该数值的选择次数类进行决策

就上面的连续段,然后决策选择 0 次、选择 1 次、选择 2 次 … 从而确保不会出现重复

Code

class Solution {
    
    
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    int n;
    public List<List<Integer>> subsetsWithDup(int[] nums) {
    
    
        n = nums.length;
        Arrays.sort(nums);
        dfs(nums, 0);
        return res;
    }
    public void dfs(int[] nums, int u){
    
    
        if(u == n){
    
    
            res.add(new ArrayList(path));
            return ;
        }

        // 找出重复段
        int t = nums[u];
        int last = u;
        while(last < n && nums[last] == nums[u]) last ++;

        // 选:
        // 决策选择不同个数的 t 的情况:选择 1 个、2 个、3 个 ... k 
        for(int i = u; i < last; i ++){
    
    
            path.add(nums[i]);
            dfs(nums, last);
        }
        // 回溯
        for(int i = u; i < last; i ++){
    
    
            path.remove(path.size() - 1);
        }

        //不选: 选择0个
        dfs(nums, last);

    }
}

猜你喜欢

转载自blog.csdn.net/qq_54773252/article/details/127285946