代码随想录 day 28

复原IP地址

给定一个只包含数字的字符串,复原它并返回所有可能的 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 地址。

示例 1:

  • 输入:s = “25525511135”
  • 输出:[“255.255.11.135”,“255.255.111.35”]

示例 2:

  • 输入:s = “0000”
  • 输出:[“0.0.0.0”]

示例 3:

  • 输入:s = “1111”
  • 输出:[“1.1.1.1”]

示例 4:

  • 输入:s = “010010”
  • 输出:[“0.10.0.10”,“0.100.1.0”]

示例 5:

  • 输入:s = “101023”
  • 输出:[“1.0.10.23”,“1.0.102.3”,“10.1.0.23”,“10.10.2.3”,“101.0.2.3”]

提示:

  • 0 <= s.length <= 3000
  • s 仅由数字组成

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

  List<String> result = new ArrayList<>();

    public List<String> restoreIpAddresses(String s) {
        backTracking(s,0,0);
        return result;
    }

    private void backTracking(String s, int startIndex,int pointNum) {
        if (pointNum==3){
            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);
                pointNum++;
                backTracking(s,i+2,pointNum);
                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;
    }

子集

给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。

说明:解集不能包含重复的子集。

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

78. 子集 - 力扣(LeetCode)

 private   List<List<Integer>> list = new ArrayList<>();
    private  LinkedList<Integer> integerList = new LinkedList<>();
    public List<List<Integer>> subsets(int[] nums) {
        backTracking(nums,0);
        return list;
    }
    private void backTracking(int[] nums,int startIndex){
        list.add(new ArrayList<>(integerList));
        if (startIndex>=nums.length){
            return;
        }
        for (int i = startIndex; i < nums.length; i++) {
            integerList.add(nums[i]);
            backTracking(nums,i+1);
            integerList.removeLast();
        }
    }

子集II

给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。

说明:解集不能包含重复的子集。

示例:

  • 输入: [1,2,2]
  • 输出: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]

90. 子集 II - 力扣(LeetCode)

List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
LinkedList<Integer> path = new LinkedList<>();// 用来存放符合条件结果
boolean[] used;
public List<List<Integer>> subsetsWithDup(int[] nums) {
    if (nums.length == 0){
        result.add(path);
        return result;
    }
    Arrays.sort(nums);
    used = new boolean[nums.length];
    subsetsWithDupHelper(nums, 0);
    return result;
}

private void subsetsWithDupHelper(int[] nums, int startIndex){
    result.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]){
            continue;
        }
        path.add(nums[i]);
        used[i] = true;
        subsetsWithDupHelper(nums, i + 1);
        path.removeLast();
        used[i] = false;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值