代码随想录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();
        }
    }
}
相关推荐
python算法(魔法师版)22 分钟前
基于机器学习鉴别中药材的方法
深度学习·线性代数·算法·机器学习·支持向量机·数据挖掘·动态规划
JNU freshman1 小时前
力扣第435场周赛讲解
算法·leetcode·蓝桥杯
眼镜哥(with glasses)1 小时前
蓝桥杯python基础算法(2-2)——基础算法(B)——模拟(上)
算法
赵鑫亿3 小时前
7.DP算法
算法·dp
iqay3 小时前
【C语言】填空题/程序填空题1
c语言·开发语言·数据结构·c++·算法·c#
还有糕手3 小时前
算法【有依赖的背包】
算法·动态规划
pursuit_csdn4 小时前
力扣 347. 前 K 个高频元素
算法·leetcode
wen__xvn4 小时前
每日一题洛谷B3865 [GESP202309 二级] 小杨的 X 字矩阵c++
c++·算法·矩阵
makabaka_T_T4 小时前
25寒假算法刷题 | Day1 | LeetCode 240. 搜索二维矩阵 II,148. 排序链表
数据结构·c++·算法·leetcode·链表·矩阵
辞半夏丶北笙5 小时前
最近最少使用算法(LRU最近最少使用)缓存替换算法
java·算法·缓存