代码随想录算法训练营-day28-93.复原IP地址、78.子集、90.子集II

93.复原IP地址

  1. 学习文章链接:
  2. 思路:见注释
  3. 代码:
class Solution {
    List<String> res = new ArrayList<>();
    public List<String> restoreIpAddresses(String s) {
        backtracking(s, 0, 0);
        return res;
    }
    public void backtracking(String s, int startIndex, int pointNum) {
        //当点数增加到3时,就将当前结果加入到结果集中
        if (pointNum == 3) {
            //这里增加判断的目的是判断最后一段是否符合要求。
            if (isValid(s, startIndex, s.length() - 1)) {
                res.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);   //将“.”加入其中
                pointNum++;                                             //点数++
                backtracking(s, i + 2, pointNum);                       //递归
                pointNum--;                                             //点数--
                s = s.substring(0, i + 1) + s.substring(i + 2);         //回溯处理,把已插入的“.”给删除掉。
            } else {
                break;
            }
        }
    }
    //判断所截取的字符串是否在0-255之间,同时排除了任意数字之前带零的情况,比如01,这种格式是错误的。
    public boolean isValid(String s, int start, int end) {
        if (start > end) return false;          //没有字符串可截取
        if (s.charAt(start) == '0' && start != end) return false; //排除01这种错误格式
        int num = 0;
        for (int i = start; i <= end; i++) {
            if (s.charAt(i) > '9' || s.charAt(i) < '0') return false; //单位数应在0-9之间
            num = 10 * num + (s.charAt(i) - '0');   //计算加起来之后为多少,例如255
            if (num > 255) return false;          //大于255则排除掉。
        }
        return true;
    }
}

78.子集

  1. 学习文章链接:
  2. 思路:见注释
  3. 代码:
class Solution {
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> subsets(int[] nums) {
        backtracking(nums, 0);
        return res;
    }
    public void backtracking(int[] nums, int startIndex) {
        res.add(new ArrayList<>(path)); //既然是子集,那么每递归一次就要将path加入到res中。
        if (startIndex >= nums.length) {
            return;
        }
        //这里倒是蛮简单吧。
        for (int i = startIndex; i < nums.length; i++) {
            path.add(nums[i]);
            backtracking(nums, i + 1);
            path.removeLast();
        }
    }
}

90.子集II

  1. 学习文章链接:
  2. 思路:这题主要基于78.子集,增加used数组去重功能,当使用used去重的时候,记得要对数组进行排序。
  3. 代码:
class Solution {
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    boolean[] used;
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);              //使用used数组去重的时候,记得要排序。
        used = new boolean[nums.length];
        backtracking(nums, 0, used);
        return res;
    }
    public void backtracking(int[] nums, int startIndex, boolean[] used) {
        res.add(new ArrayList<>(path));
        if (startIndex >= nums.length) return;
        for (int i = startIndex; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) continue;
            used[i] = true;
            path.add(nums[i]);
            backtracking(nums, i + 1, used);
            used[i] = false;
            path.removeLast();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值