算法练习第二十八天|93. 复原 IP 地址、78. 子集、90. 子集 II

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

  1. 复原 IP 地址
java 复制代码
class Solution {
    List<String> result = new ArrayList();
    public List<String> restoreIpAddresses(String s) {
        backTrace(s,0,0);
        return result;

    }

    public void backTrace(String s, int startIndex,int pointSum){
        //终止条件
        if(pointSum == 3){
            if(isValid(s,startIndex,s.length()-1))
            result.add(s);
            return;
        }

        for(int i = startIndex;i<s.length();i++){
            if(!isValid(s,startIndex,i))
            continue;
            s = s.substring(0, i + 1) + "." + s.substring(i + 1);
            pointSum +=1;
            //增加了.
            backTrace(s,i+2,pointSum);
            pointSum -=1;
             s = s.substring(0, i + 1) + s.substring(i + 2);// 回溯删掉逗点
        }

    }

    public boolean isValid(String s,int startIndex,int end){
        if (startIndex > end) {
            return false;
        }
        if (s.charAt(startIndex) == '0' && startIndex != end) { // 0开头的数字不合法
            return false;
        }
        int num = 0;
        for (int i = startIndex; 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;
    }
}
  1. 子集
java 复制代码
class Solution {
    List<Integer> path = new ArrayList();
    List<List<Integer>> result = new ArrayList();
    public List<List<Integer>> subsets(int[] nums) {
        backTrace(nums,0);
        return result;
    }

    public void backTrace(int[] nums,int startIndex){
        //搜集每个节点
        result.add(new ArrayList(path));
        if(startIndex == nums.length) return;

        for(int i = startIndex;i<nums.length;i++){
            path.add(nums[i]);
            backTrace(nums,i+1);
            path.removeLast();
        }

    }
}
  1. 子集 II
java 复制代码
class Solution {
    List<List<Integer>> result = new ArrayList();
    List<Integer> path = new ArrayList();
    boolean[] used  =null;
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        used = new boolean[nums.length];
        Arrays.fill(used,false);
        Arrays.sort(nums);
        backTrace(nums,0,used);
        return result;
    }

    public void backTrace(int[] nums,int startIndex,boolean[] used){
        result.add(new ArrayList(path));
        //终止条件
        if(startIndex == nums.length){
            return;
        }

        for(int i = startIndex;i<nums.length;i++){
            if(i>=1&&nums[i] == nums[i-1]&&!used[i-1]) continue;
            path.add(nums[i]);
            used[i] = true;
            backTrace(nums,i+1,used);
            used[i] = false;
            path.removeLast();
            
            
        }
    }
}
相关推荐
你知道网上冲浪吗18 分钟前
【原创理论】Stochastic Coupled Dyadic System (SCDS):一个用于两性关系动力学建模的随机耦合系统框架
python·算法·数学建模·数值分析
地平线开发者2 小时前
征程 6 | PTQ 精度调优辅助代码,总有你用得上的
算法·自动驾驶
Tisfy2 小时前
LeetCode 837.新 21 点:动态规划+滑动窗口
数学·算法·leetcode·动态规划·dp·滑动窗口·概率
CoovallyAIHub3 小时前
为高空安全上双保险!无人机AI护航,YOLOv5秒判安全带,守护施工生命线
深度学习·算法·计算机视觉
huangzixuan10073 小时前
08.18总结
算法·深度优先·图论
liang_jy3 小时前
数组(Array)
数据结构·面试·trae
逆向菜鸟3 小时前
【摧毁比特币】椭圆曲线象限细分求k-陈墨仙
python·算法
DolphinDB3 小时前
DolphinDB 回测插件快速上手
算法
利刃大大3 小时前
【动态规划:路径问题】最小路径和 && 地下城游戏
算法·动态规划·cpp·路径问题
武大打工仔4 小时前
用 Java 复现哲学家就餐问题
算法