回溯4| 93.复原IP地址|78.子集|90.子集II

回溯4| 93.复原IP地址|78.子集|90.子集II

一、 93.复原IP地址

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

  1. 判断一个子串是否合法时,考虑以下三点:以0为开头的数字不合法;里面有非正整数字符不合法;子串如果大于255了不合法。

  2. 本题明确要求只会分成4段,所以不能用切割线切到最后作为终止条件,而是分割的段数作为终止条件。

    pointSum表示逗点数量,pointSum为3说明字符串分成了4段了。

  3. 递归调用时,下一层递归的index要从i+2开始(因为需要在字符串中加入了分隔符.),同时记录分割符的数量pointSum 要 +1。

class Solution {
    List<String> res = new ArrayList<>();
    public List<String> restoreIpAddresses(String s) {
        if(s.length() > 12) return res;//剪枝
        backTracking(s,0,0);
        return res;
    }

    // index: 搜索的起始位置, pointSum:添加逗点的数量
    public void backTracking(String s, int index, int pointSum){
        if(pointSum == 3){
            if(isValid(s, index, s.length() -1)){
                res.add(s);
                return;
            }
        }
        for(int i = index; i < s.length(); i++){
            if(isValid(s, index, i)){
                s = s.substring(0, i + 1) + "." + s.substring(i + 1);
                pointSum += 1;
                backTracking(s, i + 2, pointSum);
                pointSum -= 1;
                s = s.substring(0, i + 1) + s.substring(i + 2);
            }else {
                break;
            }
        }
    }

    public Boolean isValid(String s, int start, int end){
        if(start > end) return false;
        if(s.charAt(start) == '0' && start != end) return false;
        int num = 0;
        for(int i = start; i <= end; i++){
            if(s.charAt(i) > '9' || s.charAt(i) < '0')  return false;
            num = num * 10 + (s.charAt(i) - '0');
            if(num > 255) return false;
        }
        return true;
    }
}

二、78.子集

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

  1. 如果把 子集问题、组合问题、分割问题都抽象为一棵树的话, 那么组合问题和分割问题都是收集树的叶子节点,而子集问题是找树的所有节点!
  2. 终止条件:index已经大于数组的长度了,就终止了,因为没有元素可取了 。其实可以不需要加终止条件,因为startIndex >= nums.size(),本层for循环本来也结束了。
  3. 注意收集结果 res.add(new ArrayList<>(path)) 要放在终止条件的前面,先收集结果,再return.
class Solution {
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> subsets(int[] nums) {
        backTracking(nums, 0);
        return res;
    }

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

三、90.子集II

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

  1. 用数组used 标记 nums 中的元素是否被使用过。used[i - 1] == true,说明同一树枝nums[i - 1]使用过;used[i - 1] == false,说明同一树层nums[i - 1]使用过。
  2. 思路:先对数组排序,判断当前数字是否被使用过,若使用过则跳过。
class Solution {
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    boolean[] used;
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        if(nums.length == 0){
            res.add(new ArrayList<>(path));
            return res;
        }
        Arrays.sort(nums);
        used = new boolean[nums.length];
        Arrays.fill(used,false);
        backTracking(nums, 0);
        return res;
    }

    public void backTracking(int[] nums, int index){
        res.add(new ArrayList<>(path));
        if(index >= nums.length) return;
        for(int i = index; i < nums.length; i++){
            if(i > 0 && nums[i] == nums[i - 1] && !used[i - 1]){
                continue;
            }
            path.add(nums[i]);
            used[i] = true;
            backTracking(nums,i + 1);
            used[i] = false;
            path.removeLast();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值