首页 > 编程语言 >LeetCode算法训练 93.复原IP地址 78.子集 90.子集II

LeetCode算法训练 93.复原IP地址 78.子集 90.子集II

时间:2023-02-27 21:56:45浏览次数:48  
标签:return nums int II startIndex 子集 path LeetCode

欢迎关注个人公众号:爱喝可可牛奶

LeetCode算法训练 93.复原IP地址 78.子集 90.子集II

LeetCode 93. 复原 IP 地址

分析

字符串全部由数字组成,ipv4每一段数字不能有前导0,且大小∈[0,255]

等价于将字符串进行分割,并判断分割后的数是否满足条件

插入一个点进行切割、判断是否满足条件、再插入、再判断,直到插入3个点,判断剩下的一段是否满足条件

代码

class Solution {
    List<String> res = new ArrayList<>();

    public List<String> restoreIpAddresses(String s) {
        if (s.length() > 12) return res; // 算是剪枝了
        backTrack(s, 0, 0);
        return res;
    }

    // startIndex: 搜索的起始位置, pointNum:添加逗点的数量
    private void backTrack(String s, int startIndex, int pointNum) {
        if (pointNum == 3) {// 逗点数量为3时,分隔结束
            // 判断第四段⼦字符串是否合法,如果合法就放进res中
            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);    //在str的后⾯插⼊⼀个逗点
                pointNum++;
                backTrack(s, i + 2, pointNum);// 插⼊逗点之后下⼀个⼦串的起始位置为i+2
                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;
    }
}

LeetCode 78. 子集

分析

返回不含相同元素整数数组的子集

收集树的每个节点

代码

class Solution {
    List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
    LinkedList<Integer> path = new LinkedList<>();// 用来存放符合条件结果
    public List<List<Integer>> subsets(int[] nums) {
        subsetsHelper(nums, 0);
        return result;
    }

    private void subsetsHelper(int[] nums, int startIndex){
        //「遍历这个树的时候,把所有节点都记录下来,就是要求的子集集合」。
        result.add(new ArrayList<>(path));
        if (startIndex >= nums.length){ //终止条件可不加
            return;
        }
        for (int i = startIndex; i < nums.length; i++){
            path.add(nums[i]);
            subsetsHelper(nums, i + 1);
            path.removeLast();
        }
    }
}

LeetCode 90. 子集 II

分析

返回含相同元素整数数组的子集 在前面基础上去重

代码

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

    private void subsetsHelper(int[] nums, int startIndex){
        //「遍历这个树的时候,把所有节点都记录下来,就是要求的子集集合」。
        result.add(new ArrayList<>(path));
        if (startIndex >= nums.length){ //终止条件可不加
            return;
        }
        for (int i = startIndex; i < nums.length; i++){
            // 注意这里不是0
            //if(i > 0 && nums[i] == nums[i-1]){
            if(i > startIndex && nums[i] == nums[i-1]){
                continue;
            }
            path.add(nums[i]);
            subsetsHelper(nums, i + 1);
            path.removeLast();
        }
    }
}

总结

  1. 涉及范围确定,明确开闭区间
  2. 去重方式 Set去重、used数组去重、索引去重

标签:return,nums,int,II,startIndex,子集,path,LeetCode
From: https://www.cnblogs.com/cupxu/p/17162057.html

相关文章

  • leetcode-1024-easy
    DivisorGameAliceandBobtaketurnsplayingagame,withAlicestartingfirst.Initially,thereisanumbernonthechalkboard.Oneachplayer'sturn,that......
  • leetcode-1037-easy
    ValidBoomerangGivenanarraypointswherepoints[i]=[xi,yi]representsapointontheX-Yplane,returntrueifthesepointsareaboomerang.Aboomerang......
  • 【LeetCode二叉树#10】从中序与后序遍历序列构造二叉树
    力扣题目链接(opensnewwindow)根据一棵树的中序遍历与后序遍历构造二叉树。注意:你可以假设树中没有重复的元素。例如,给出中序遍历inorder=[9,3,15,20,7]后序遍......
  • 代码随想录算法训练营Day27 回溯算法|39. 组合总和 40.组合总和II 131.分割回文串
    代码随想录算法训练营39.组合总和题目链接:39.组合总和给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 targ......
  • #yyds干货盘点# LeetCode面试题:串联所有单词的子串
    1.简述:给定一个字符串 s 和一个字符串数组 words。 words 中所有字符串长度相同。 s 中的串联子串是指一个包含 words 中所有字符串以任意顺序排列连接起来的......
  • NIOS II与LCD1602
    使用NIOS内置LCD16207IP核驱动LCD1602闲话为了在NIOS中驱动LCD1602,走了很多的弯路,网上搜索了很多,但是这方面的资料太少了。在官方文档《EmbeddedPeripheralsIPUserG......
  • LeetCode 79. 单词搜索(/dfs)
    原题解题目约束题解classSolution{public:boolcheck(vector<vector<char>>&board,vector<vector<int>>&visited,inti,intj,string&s,intk)......
  • LeetCode 周赛 334,在算法的世界里反复横跳
    本文已收录到AndroidFamily,技术和职场问题,请关注公众号[彭旭锐]提问。大家好,我是小彭。今天是LeetCode第334场周赛,你参加了吗?这场周赛考察范围比较基础,整体难度......
  • LeetCode 78. 子集(/)
    原题解题目约束题解解法一classSolution{public:vector<int>t;vector<vector<int>>ans;vector<vector<int>>subsets(vector<int>&nums)......
  • iis7上部署netcore项目的步骤
    1、安装AspNetCoreModule托管模块(选择.NetCore3.1版本)AspNetCoreModule下载地址:https://dotnet.microsoft.com/download/dotnet-core1安装下面两个文件dotnet-sdk-3......