代码随想录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();
        }
    }
}
相关推荐
不是吧这都有重名1 小时前
[论文阅读]Deeply-Supervised Nets
论文阅读·人工智能·算法·大语言模型
homelook1 小时前
matlab simulink双边反激式变压器锂离子电池均衡系统,双目标均衡策略,仿真模型,提高均衡速度38%
算法
什码情况2 小时前
星际篮球争霸赛/MVP争夺战 - 华为OD机试真题(A卷、Java题解)
java·数据结构·算法·华为od·面试·机试
天上路人2 小时前
采用AI神经网络降噪算法的通信语音降噪(ENC)模组性能测试和应用
人工智能·神经网络·算法
字节高级特工2 小时前
【C++】”如虎添翼“:模板初阶
java·c语言·前端·javascript·c++·学习·算法
.Vcoistnt2 小时前
Codeforces Round 1024 (Div. 2)(A-D)
数据结构·c++·算法·贪心算法·动态规划·图论
晴天下小雨o2 小时前
排序算法总结
java·算法·排序算法
程序员爱钓鱼3 小时前
循环语句:for、range -《Go语言实战指南》
java·数据结构·算法
LabVIEW开发3 小时前
LabVIEW中算法开发的系统化解决方案与优化
算法·labview
chenyuhao20244 小时前
链表面试题7之相交链表
数据结构·算法·链表·面试·c#