【代码随想录算法训练营Day27】93.复原IP地址;78.子集;90.子集II

文章目录

❇️Day 27 第七章 回溯算法 part04

✴️今日内容

  • 93.复原IP地址
  • 78.子集
  • 90.子集II

❇️93.复原IP地址

自己的思路

切割问题可以抽象为树型结构,如图:

自己的代码

不需要用path存储答案了,只需要在原数组s上插入'.'

java 复制代码
class Solution {
    List<String> res = new ArrayList<>();
    public List<String> restoreIpAddresses(String s) {
            //利用StringBuilder类型好操作
        StringBuilder sb = new StringBuilder(s);
        dfs(sb, 0, 0);
        return  res;
    }
    //dotCount: 记录添加逗点的数量
    public void dfs(StringBuilder s, int startIndex, int dotCount){
        if(dotCount == 3){
            if(isValid(s, startIndex, s.length() - 1)){
                res.add(s.toString());
            }
            return;
        }
        for (int i = startIndex; i < s.length(); i++) {
            //判断分割出来的子串是否合法
            if(isValid(s, startIndex, i)){
                s.insert(i + 1, '.');
                dfs(s, i + 2, dotCount + 1);
                s.deleteCharAt(i + 1);        //回溯
            }else break;
        }
        return;
    }
    //[start, end]
    private boolean isValid(StringBuilder s, int start, int end) {
        if (start > end) {
            return false;
        }
        if (s.charAt(start) == '0' && start != end) { // 0开头的数字不合法
            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) { // 如果⼤于255了不合法
                return false;
            }
        }
        return true;
    }
}

❇️78.子集

(3.8补)有点忘了回溯法了,看了下文章思路

随想录思路

以示例中nums = [1,2,3]为例把求子集抽象为树型结构,如下:

从图中红线部分,可以看出遍历这个树的时候,把所有节点都记录下来,就是要求的子集集合。

自己的思路

类似组合

  1. 确定递归函数的参数和返回值
    1. nums
    2. startIndex:确定开始下标
  2. 确定终止条件
    1. 集合为空
  3. 确定单层递归的逻辑

自己的代码

严格按照模板来,和组合逻辑一样,只是添加逻辑不一样

java 复制代码
class Solution {
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> subsets(int[] nums) {
        dfs(nums, 0);
        return res;
    }
    //可以把dfs函数改成void
    public List<List<Integer>> dfs(int[] nums, int startIndex){
        res.add(new ArrayList<>(path));
        if(startIndex >= nums.length) return res;
        for (int i = startIndex; i < nums.length; i++) {
            path.push(nums[i]);
            dfs(nums, i + 1);
            path.pop();
        }
        return res;
    }
}

❇️90.子集II

自己的思路

和上一题一样只需要遇到相同数时判断一下前面相同的数是否被用过,所以添加参数boolean visited[]判断数有没有被用过

自己的代码

java 复制代码
class Solution {
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    boolean[] visited;
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        if (nums.length == 0){
            res.add(path);
            return res;
        }
        Arrays.sort(nums);
        visited = new boolean[nums.length];
        dfs(nums, 0);
        return res;
    }
    public void dfs(int[] nums, int start){
        res.add(new ArrayList<>(path));
        if(start >= nums.length) {
            return;
        }
        for (int i = start; i < nums.length; i++) {
            if(i > 0 && nums[i] == nums[i - 1] && !visited[i - 1]){
                continue;
            }
            visited[i] = true;
            path.push(nums[i]);
            dfs(nums, i + 1);
            path.pop();
            visited[i] = false;
        }
    }
}
相关推荐
成工小白4 分钟前
【Linux】进程地址空间
linux·算法
凤年徐4 分钟前
【C/C++】自定义类型:结构体
c语言·开发语言·c++·经验分享·笔记·算法
Inverse1621 小时前
C语言_自定义类型:结构体
c语言·开发语言·算法
Musennn1 小时前
102. 二叉树的层序遍历详解:队列操作与层级分组的核心逻辑
java·数据结构·算法·leetcode
越来越无动于衷1 小时前
java数组题(5)
java·算法
理论最高的吻1 小时前
77. 组合【 力扣(LeetCode) 】
c++·算法·leetcode·深度优先·剪枝·回溯法
强盛小灵通专卖员4 小时前
分类分割详细指标说明
人工智能·深度学习·算法·机器学习
IT猿手8 小时前
基于强化学习 Q-learning 算法求解城市场景下无人机三维路径规划研究,提供完整MATLAB代码
神经网络·算法·matlab·人机交互·无人机·强化学习·无人机三维路径规划
万能程序员-传康Kk11 小时前
旅游推荐数据分析可视化系统算法
算法·数据分析·旅游
PXM的算法星球11 小时前
【并发编程基石】CAS无锁算法详解:原理、实现与应用场景
算法