代码随想录Day24

目录

93.复原IP地址

78.子集

90.子集II


93.复原IP地址

本题明确要求只会分成4段,所以不能用切割线切到最后作为终止条件,而是分割的段数作为终止条件。

注意注释部分。

java 复制代码
class Solution {
    List<String> res = new ArrayList<>();

    public List<String> restoreIpAddresses(String s) {
        StringBuilder s1 = new StringBuilder(s);
        fun(s1, 0, 0);
        return res;
    }

    public void fun(StringBuilder s,int index,int count){
       // System.out.println(s+" count:"+count);
        if(count==3){
            if(check(s,index,s.length()-1)){
                res.add(s.toString());
            }
            return;
        }
        for(int i=index;i<s.length();i++){
            if(check(s,index,i)){
                s.insert(i+1,'.');
                fun(s,i+2,count+1);//count++不行
                s.deleteCharAt(i+1);
            }else{break;}
        }
    }

    public boolean check(StringBuilder s,int start,int end){
        if(start>end) return false;
        if(s.charAt(start)=='0' && start!=end){
            return false;
        }
        int num=0;
        for(int i = start; i <= end; i++){
            int temp=s.charAt(i)-'0';
            num=num*10+temp;
            if(num>255) return false;
        }
        
        return true;
    }
}

78.子集

或者直接先记录,就可以避免需要先录入空数组

java 复制代码
class Solution {
    List<List<Integer>> result=new ArrayList<>();
    LinkedList<Integer> cur=new LinkedList<>();
    public List<List<Integer>> subsets(int[] nums) {
        result.add(new ArrayList<>());
        fun(0,nums);
        return result;
    }
    public void fun(int index,int[] nums){
        if(index>=nums.length){
            return;
        }
        for(int i=index;i<nums.length;i++){
            cur.add(nums[i]);
            result.add(new ArrayList<>(cur));
            fun(i+1,nums);
            cur.removeLast();
        }
    }
}

90.子集II

java 复制代码
class Solution {
    List<List<Integer>> res=new ArrayList<>();
    LinkedList<Integer> cur=new LinkedList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        fun(nums,0);
        return res;
    }
    public void fun(int[] nums,int index){
        res.add(new ArrayList<>(cur));
        if(index>=nums.length){
            return;
        }
        for(int i=index;i<nums.length;i++){
            if(i>index &&nums[i]==nums[i-1]) continue;//注意是大于index,而不是大于0
            cur.add(nums[i]);            
            fun(nums,i+1);          
            cur.removeLast();
        }
    }
}
相关推荐
开源盛世!!几秒前
3.9-3.11学习笔记
数据结构·算法
乌萨奇也要立志学C++3 分钟前
【洛谷】图论 最小生成树详解:Prim与Kruskal算法(含代码实现)
算法·图论
智者知已应修善业4 分钟前
【花费最少钱加油到最后(样例数据推敲)】2024-11-18
c语言·c++·经验分享·笔记·算法
飞Link15 分钟前
深度解析 NT-Xent:对比学习中的标准化温度交叉熵损失
python·算法·数据挖掘·回归
饿了就去喝水18 分钟前
C语言笔试程序题
c语言·数据结构·算法
故事和你9121 分钟前
sdut-程序设计基础Ⅰ-实验三while循环(1-10)
开发语言·数据结构·c++·算法·类和对象
再一次等风来23 分钟前
声源定位算法5----SRP-PHAT(1)
算法·信号处理·srp
Yupureki25 分钟前
《算法竞赛从入门到国奖》算法基础:数据结构-并查集
c语言·数据结构·c++·算法
DeepModel25 分钟前
【概率分布】伯努利分布详解
算法·概率论
再一次等风来26 分钟前
声源定位算法5----SRP-PHAT(2)
算法·信号处理·srp·声源定位·gcc-phat