代码随想录Day30

93.复原IP地址

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

思路:这个跟分割字符串是一样的,需要多一个判断数字是否在0-255的函数,不能含有前导0也是一个关键

尝试
class Solution {
    List<String> result = new ArrayList<>();
    StringBuilder temp = new StringBuilder();
    public List<String> restoreIpAddresses(String s) {
        backTracking(s,0);
        return result;
    }
    public void backTracking(String s,int startIndex){
        if(startIndex >= s.length()){
            result.add(temp.toString());
            return;
        }
        for(int i = startIndex; i < s.length(); i++){
            String str = s.substring(startIndex,i+1);
            int num = Integer.parseInt(str);
            if(num>=0 && num <= 255){
                temp.append(str);
                temp.append('.');
            }else{
                continue;
            }
            backTracking(s,i+1);
            temp.setLength(temp.length() -1);
        }
    }
}
答案
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]所组成的数字是否合法
    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;
    }
}
复习
class Solution {
    List<String> result = new ArrayList<>();
    public List<String> restoreIpAddresses(String s) {
        if(s.length() > 12) return result;
        backTracking(s,0,0);
        return result;
    }
    public void backTracking(String s,int startIndex,int pointNum){
        if(pointNum == 3){
            result.add(s);
        }
        for(int i = startIndex; i < s.length(); i++){
            if(isValid(s,startIndex,i)){
                s = s.substring(startIndex,i+1) + "." + s.substring(i+1);
                pointNum++;
                backTracking(s,i+1,pointNum);
                pointNum--;
                s = s.substring(startIndex,i+1) + s.substring(i+2);
            }else{
                break;
            }
        }
    }
    public boolean isValid(String s,int begin,int end){
        if(begin > end) return false;
        if(s.charAt(begin)=='0' && begin!=end) return false;
        for(int i = begin; i < end; i++){
            if(s.charAt(i) > '9' || s.charAt(i) < '0'){
                return false;
            }
        }
        return true;
    }
}

 要把字符串转换成数字进行比较,才能知道是否合法

小结
  • 注意,IP需要加上【.】进行分割,所以要有一个参数记录【.】的数量
  • 天才!直接在原字符串s里面加上逗点,resullt直接存改造之后的s就行,根本不用stringBuilder 
  • s.substring(i + 2):这个调用从索引 i + 2 开始,直到字符串的末尾。s.substring(0, i + 1):这个调用从索引 0 开始,直到(但不包括)索引 i + 1

78.子集

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

思路:按照取n个来作为分支,终止条件就看也没有取到n个元素,所以是终止条件一直在变的,这样for循环第一层和第二层就不一样了,难搞

尝试
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> son = new ArrayList<>();
    public List<List<Integer>> subsets(int[] nums) {
        backTracking(nums,0);
        return result;
    }
    public void backTracking(int[] nums,int startIndex){
        result.add(new ArrayList<>(son));
        if(startIndex > nums.length){
            return;
        }
        for(int i = startIndex; i < nums.length; i++){
            son.add(i);
            backTracking(nums,startIndex);
            son.remove(son.size() - 1);
        }
    }
}
答案
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> son = new ArrayList<>();
    public List<List<Integer>> subsets(int[] nums) {
        backTracking(nums,0);
        return result;
    }
    public void backTracking(int[] nums,int startIndex){
        result.add(new ArrayList<>(son));
        for(int i = startIndex; i < nums.length; i++){
            son.add(nums[i]);
            backTracking(nums,i + 1);
            son.remove(son.size() - 1);
        }
    }
}
小结
  1. 终止条件可以不加,【组合总和】收集叶子节点,这个相当于是收集所有节点
  2. 是要收集【 nums[i] 】,而不是【i】
  3. 递归时,要传入【i】,传【startIndex】是不会变的!

90.子集||

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

思路:跟【组合总和||】是一样的道理,注意数组剪枝前要排序,以及for循环要跳过相同数字,树层去重

尝试(看了下组合总和,AC)
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> son = new ArrayList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        backTracking(nums,0);
        return result;
    }
    public void backTracking(int[] nums,int startIndex){
        result.add(new ArrayList<>(son));
        for(int i = startIndex; i < nums.length; i++){
            if(i > startIndex && nums[i] == nums[i-1] ){
                continue;
            }
            son.add(nums[i]);
            backTracking(nums,i+1);
            son.remove(son.size() - 1);
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值