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

93. 复原 IP 地址:

题目链接
有效 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 ‘.’ 分隔。

例如:“0.1.2.201” 和 “192.168.1.1” 是 有效 IP 地址,但是 “0.011.255.245”、“192.168.1.312” 和 “192.168@1.1” 是 无效 IP 地址。
给定一个只包含数字的字符串 s ,用以表示一个 IP 地址,返回所有可能的有效 IP 地址,这些地址可以通过在 s 中插入 ‘.’ 来形成。你 不能 重新排序或删除 s 中的任何数字。你可以按 任何 顺序返回答案。

示例 :

输入:s = "25525511135"
输出:["255.255.11.135","255.255.111.35"]

解答:

class Solution {
    List<String> res = new ArrayList<>();
    public List<String> restoreIpAddresses(String s) {
        backtrack(s,0,0);
        return res;
    }
    public void backtrack(String s,int count,int index){
        if(count==3){
            if (isIp(s,index,s.length()-1)) {
                res.add(s);
            }
            return;
        }

        for (int i = index; i <s.length() ; i++) {
            if(isIp(s,index,i)){
                s = s.substring(0, i + 1) + "." + s.substring(i + 1);
                count++;
                backtrack(s,count,i+2);
                s = s.substring(0, i + 1) + s.substring(i + 2);
                count--;

            }else{
                break;
            }
        }
    }
    public boolean isIp(String s,int start, int end){
        if(start>end){
            return false;
        }
        if(s.charAt(start)=='0'&&start!=end){
            return false;
        }
        int sum = 0;
        for (int i = start; i <=end ; i++) {
            if(s.charAt(i) > '9' || s.charAt(i) < '0'){
                return false;
            }
        sum = sum * 10 + (s.charAt(i) - '0');
        }
        if(sum<0||sum>255){
            return false;
        }
        return true;
    }
}

算法总结:

复原ip地址这题和我们之前遇到的回文题很像,不同点在于,ip的格式需要我们处理,同时我们以“.”的数量来作为递归终止条件。

78. 子集:

题目链接
给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

示例 :

输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

解答:

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> subsets(int[] nums) {
        backtrack(nums,0);
        return res;
    }
    public void backtrack(int[] nums,int index){
        res.add(new ArrayList<>(path));
        for (int i = index; i <nums.length ; i++) {
            path.add(nums[i]);
            backtrack(nums,i+1);
            path.remove(path.size()-1);
        }
    }
}

算法总结:

子集问题和之前的组合问题最大的区别是我们在递归过程中每个子集都应该加入我们的结果集中,则res.add(new ArrayList<>(path));在本题中我们不限制条件。

90. 子集 II:

题目链接
给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。

示例 :

输入:nums = [1,2,2]
输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]

解答:

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        int[] used = new int[nums.length];
        Arrays.sort(nums);
        Arrays.fill(used,0);
        backtrack(used,nums,0);
        return res;
    }
    public void backtrack(int[] used,int[] nums,int index){
        res.add(new ArrayList<>(path));
        for (int i = index; i <nums.length ; i++) {
            if(i>0&&nums[i]==nums[i-1]&&used[i-1]==0){
                continue;
            }
            
            path.add(nums[i]);
            used[i] = 1;
            backtrack(used,nums,i+1);
            path.remove(path.size()-1);
            used[i] = 0;
        }
    }
}

算法总结:

子集Ⅱ和子集Ⅰ的区别类似于先前的组合问题,我们同样可以用一个used数组记录重复的情况,判断条件为i>0&&nums[i]==nums[i-1]&&used[i-1]==0,即当前数字和前面的数字相同,且前面一个数字没有被添加的情况我们跳过循环(可以想象一个若是1,2已经添加,我们不进行used[i-1]==0判断则会错过1,2,2这种情况)

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值