代码随想录算法训练营第二十八天| 93.复原IP地址、 78.子集、90.子集II

93.复原IP地址

参考文章:代码随想录 

参考视频:回溯算法如何分割字符串并判断是合法IP?| LeetCode:93.复原IP地址_哔哩哔哩_bilibili 

解题思路:

本题和上题 131.分割回文串 做法基本是一致的,不过需要注意startIndex的初始位置,以及对判断子字符串s是否符合标准的函数逻辑。

确定递归函数参数,传入字符串s,分割符startIndex,逗点数量pointNum。

确定终止条件,当逗点数量为3时,判断剩余部分是否符合标准,符合则将整个字符串添加到结果集中,最后统一return。

确定单层递归逻辑,for循环遍历字符串s,判断s到startIndex之间的子字符串是否符合标准,若不符合直接break退出循环,若符合,在字符串s的切割符处添加逗点,pointNum++,进入到下一次递归,注意传入的startIndex的应该为startIndex+2,因为加了一个逗点。

public class Leetcode93 {
    List<String> res = new ArrayList<>();

    public List<String> restoreIpAddresses(String s) {
        track(s, 0, 0);
        return res;
    }

    public void track(String s, int startIndex, int pointNum) {
        if (pointNum == 3) {
            if (isValid(s, startIndex, s.length() - 1)) {
                res.add(s);
            }
            return;
        }

        for (int i = startIndex; i < s.length(); i++) {
            if (isValid(s, startIndex, i)) {
                s = s.substring(0, i + 1) + '.' + s.substring(i + 1);
                pointNum++;
                track(s, i + 2, pointNum);
                pointNum--;
                s = s.substring(0, i + 1) + s.substring(i + 2);
            } else {
                break;
            }
        }
    }

    public boolean isValid(String s, int left, int right) {
        if (left > right) {
            return false;
        }
        if (s.charAt(left) == '0' && left != right) {
            return false;
        }
        int num = 0;
        for (int i = left; i <= right; i++) {
            if (s.charAt(i) < '0' || s.charAt(i) > '9') {
                return false;
            }
            num = num * 10 + (s.charAt(i) - '0');
            if (num > 255) { // 如果⼤于255了不合法
                return false;
            }
        }
        return true;
    }
}

 78.子集

参考文章:代码随想录 

参考视频:回溯算法解决子集问题,树上节点都是目标集和! | LeetCode:78.子集_哔哩哔哩_bilibili 

解题思路:

将求子集抽象为树形结构的话,可以看出我们需要收集所有的节点,而不仅仅是叶子节点。

确定递归函数参数,传入数组nums,for循环遍历起始位置startIndex。

确定终止条件,当startIndex大于等于数组长度时,结束。

确定单层递归逻辑,用for循环遍历数组,将每个元素添加到路径集path中,继续递归,需要注意本题添加路径到结果集的操作可以写到递归函数开头,这样可以保证每个节点都能够获取到。

public class Leetcode78 {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();

    public List<List<Integer>> subsets(int[] nums) {
        track(nums,0);
        return res;
    }

    public void track(int[] nums, int startIndex) {
        res.add(new ArrayList<>(path));
        if (startIndex >= nums.length) {
            return;
        }

        for (int i = startIndex; i < nums.length; i++) {
            path.add(nums[i]);
            track(nums, startIndex + 1);
            path.remove(path.size() - 1);
        }
    }
}

 

90.子集II 

参考文章:代码随想录 

解题思路:

本题比较简单,其实就是78.子集 和 40.组合总和II 组合在了一起,获取每一个节点的结果存入结果集中,使用used数组来去标记已经使用过的元素来进行去重。

public class Leetcode90 {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        boolean[] used = new boolean[nums.length];
        Arrays.fill(used, false);
        track(nums, 0, used);
        return res;
    }

    public void track(int[] nums, int startIndex, boolean[] used) {
        res.add(new ArrayList<>(path));
        if (startIndex == nums.length) {
            return;
        }
        for (int i = startIndex; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) {
                continue;
            }
            path.add(nums[i]);
            used[i] = true;
            track(nums, i + 1, used);
            used[i] = false;
            path.remove(path.size() - 1);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值