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

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

93.复原IP地址

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

此题和 131. 分割回文串 - 力扣(LeetCode)一样是分割问题

93.复原IP地址

class Solution {
    List<String> result = new ArrayList<>();
    public List<String> restoreIpAddresses(String s) {
        if (s.length() > 12) return result; // 算是剪枝了
        backTrack(s, 0, 0);
        return result;
    }

    // startIndex: 搜索的起始位置, pointNum:添加逗点的数量
    private void backTrack(String s, int startIndex, int pointNum) {
        if (pointNum == 3) {// 逗点数量为3时,分隔结束
            // 判断第四段⼦字符串是否合法,如果合法就放进result中
            if (isValid(s,startIndex,s.length()-1)) {
                result.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);    //在str的后⾯插⼊⼀个逗点
                pointNum++;
                backTrack(s, i + 2, pointNum);// 插⼊逗点之后下⼀个⼦串的起始位置为i+2
                pointNum--;// 回溯
                s = s.substring(0, i + 1) + s.substring(i + 2);// 回溯删掉逗点
            } else {
                // 剪枝
                break;
            }
        }
    }

    // 判断一个段位字符串s在左闭⼜闭区间[start, end]所组成的数字是否合法
    /**
    段位以0为开头的数字不合法
	段位里有非正整数字符不合法
	段位如果大于255了不合法
    */
    private Boolean isValid(String 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.子集

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

class Solution {
    List<List<Integer>> res;
    List<Integer> buffer;
    public List<List<Integer>> subsets(int[] nums) {
        res = new ArrayList<>();
        buffer = new ArrayList<>();
        dfs(0, nums);
        return res;
    }

    private void dfs(int index, int[] nums) {
        res.add(new ArrayList<>(buffer));
        if(index == nums.length) {
            return;
        }

        for(int i = index; i < nums.length; ++i) {
            buffer.add(nums[i]);
            dfs(i + 1, nums);
            buffer.remove(buffer.size() - 1);
        }
    }
}

90.子集II

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

class Solution {
    List<List<Integer>> res;
    List<Integer> buffer;
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        res = new ArrayList<>();
        buffer = new ArrayList<>();
        Arrays.sort(nums);
        dfs(0, nums);
        return res;
    }

    private void dfs(int index, int[] nums) {
        res.add(new ArrayList<>(buffer));
        if(index == nums.length) {
            return;
        }

        for(int i = index; i < nums.length; ++i) {
            if(i > index && nums[i] == nums[i - 1]) continue;
            buffer.add(nums[i]);
            dfs(i + 1, nums);
            buffer.remove(buffer.size() - 1);
        }
    }
}

总结:

今天的难点主要是 93. 复原 IP 地址 - 力扣(LeetCode) 中的判断一个段位是否合法

}
}

}




## 总结:

今天的难点主要是 [93. 复原 IP 地址 - 力扣(LeetCode)](https://leetcode.cn/problems/restore-ip-addresses/) 中的判断一个段位是否合法



  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值