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


今日任务


93.复原IP地址
78.子集
90.子集II 
 


93.复原IP地址

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

class Solution {
    List<String> path = new ArrayList<>();
    List<String> result = new ArrayList<>();
    public List<String> restoreIpAddresses(String s) {
        retrospective(s, 0);
        return result;
    }
    private void retrospective(String s, int startIndex) {
        if (path.size() == 4 && startIndex == s.length()) {
            String str = String.join(".", path);
            result.add(str);
            return;
        }
        for (int i = startIndex; i < s.length(); i++) {
            if (isValid(s, startIndex, i)) {
                path.add(s.substring(startIndex, i + 1));
                retrospective(s, i + 1);
                path.remove(path.size() - 1);
            }
        }
        return;
    }
    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.子集

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

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


90.子集II

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

class Solution {

    List<List<Integer>>  result = new ArrayList<>();
    List<Integer>  path = new ArrayList<>();
Set<List<Integer>> set = new HashSet<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        result.add(new ArrayList<>());
        Arrays.sort(nums);
        retrospective(path, nums, 0);
        return result;
    }

    public void retrospective(List<Integer> path, int[] nums, int startIndex) {
        if (startIndex == nums.length) {return;}
        for (int i = startIndex; i < nums.length; i++) {
            path.add(nums[i]);
            ArrayList<Integer> tmpPath = new ArrayList<>(path);
            if (!set.contains(tmpPath)) {
                result.add(tmpPath);
                set.add(tmpPath);
            }
            retrospective(path, nums, i + 1);
            path.remove(path.size() - 1);
        }
        return;
    }    
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值