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

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

Leetcode 93.复原IP地址
题目链接:https://leetcode.cn/problems/restore-ip-addresses/description/
题目描述:

有效 IP 地址 正好由四个整数(每个整数位于 0255 之间组成,且不能含有前导 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 中的任何数字。你可以按 任何 顺序返回答案。

示例 1:

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

示例 2:

输入:s = "0000"
输出:["0.0.0.0"]

示例 3:

输入:s = "101023"
输出:["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]

提示:

  • 1 <= s.length <= 20
  • s 仅由数字组成
思路:

1、回溯

图示:

在这里插入图片描述

代码1:回溯
class Solution {
    List<String> res = new ArrayList<>();

    public List<String> restoreIpAddresses(String s) {
         StringBuilder sb = new StringBuilder(s);
         backTracking(sb , 0 ,0);
         return res;
    }
    public void backTracking(StringBuilder s , int startIndex , int pointSum){
        // pointSum记录逗点
        // 终止
        if(pointSum == 3){
            if(isVaild(s,startIndex,s.length() - 1)){
                // 如果合法
                res.add(s.toString());
               
            }
            return;  
        }
       
        for(int i = startIndex ; i < s.length() ; i++){
            if(isVaild(s , startIndex , i)){
                // 符合条件
                // 插入逗点
                s.insert(i + 1 , ".");
                backTracking(s , i + 2 , pointSum + 1);
                s.deleteCharAt( i + 1);
            }else{
                break;
            }
        }

    }
    public boolean isVaild(StringBuilder s , int start , int end){
        if(start > end){
            return false;
        }
        // 数字前面有0
        if(s.charAt(start) == '0' && start!=end){
            return false;
        }
        int num = 0;
        for(int i = start ; i <= end ; i++){
            int dight = s.charAt(i) - '0';
            num = num * 10 + dight;
            if( num > 255){
                return false;
            }

        }
        return true;
    }
}
总结:注意“.”的位置是上一位+1
Leetcode 78.子集
题目链接:https://leetcode.cn/problems/subsets/description/
题目描述:

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的

子集

(幂集)。

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

示例 1:

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

示例 2:

输入:nums = [0]
输出:[[],[0]]

提示:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
  • nums 中的所有元素 互不相同
思路:

1、回溯

图示:

在这里插入图片描述

代码1:回溯
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));

        // 终止条件
        if(startIndex > nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        // 单层
        for(int i = startIndex ; i < nums.length ; i++){
            path.add(nums[i]);
            // 防止子集中有重复的
            backTracking(nums , i + 1);
            path.removeLast();
        }
    }
}
总结:
Leetcode 90.子集II
题目链接:https://leetcode.cn/problems/subsets-ii/description/
题目描述:

给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的

子集

(幂集)。

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

示例 1:

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

示例 2:

输入:nums = [0]
输出:[[],[0]]

提示:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
思路:

1、回溯

图示:

在这里插入图片描述

代码1:回溯
class Solution {
    // 结果
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    boolean[] used;
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        if(nums.length == 0){
            res.add(path);
            return res;
        }
        used = new boolean[nums.length];
        Arrays.sort(nums);
        backTracking(nums , 0);
        return res;
    }
    public void backTracking(int[] nums, int startIndex){
        //防止结果漏掉
        res.add(new ArrayList<>(path));
        if(nums.length <= startIndex){
            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);
            path.removeLast();
            used[i] = false;
        }
    }
}
  • 12
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值