算法练习第二十八天|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();
            
            
        }
    }
}
相关推荐
_殊途39 分钟前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
珊瑚里的鱼4 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
秋说5 小时前
【PTA数据结构 | C语言版】顺序队列的3个操作
c语言·数据结构·算法
lifallen5 小时前
Kafka 时间轮深度解析:如何O(1)处理定时任务
java·数据结构·分布式·后端·算法·kafka
liupenglove6 小时前
自动驾驶数据仓库:时间片合并算法。
大数据·数据仓库·算法·elasticsearch·自动驾驶
python_tty7 小时前
排序算法(二):插入排序
算法·排序算法
然我7 小时前
面试官:如何判断元素是否出现过?我:三种哈希方法任你选
前端·javascript·算法
risc1234567 小时前
BKD 树(Block KD-Tree)Lucene
java·数据结构·lucene
F_D_Z7 小时前
【EM算法】三硬币模型
算法·机器学习·概率论·em算法·极大似然估计
kk_stoper7 小时前
如何通过API查询实时能源期货价格
java·开发语言·javascript·数据结构·python·能源