DAY28| 93. 复原IP地址 ,79.子集 ,90.子集II

文章目录

93.复原IP地址

文字讲解复原IP地址

视频讲解复原IP地址

**状态:**此题调试了几次ok,与昨天的分割回文子串相比,就是在判断终止条件处需要处理;

思路:

代码:

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

    public List<String> restoreIpAddresses(String s) {
        backTracking(0, s);
        return result;
    }

    public void backTracking(Integer startIndex, String s) {
        if (tempList.size() == 3) {
            if (validIpParam(s.substring(startIndex, s.length()))) {
                tempList.offer(s.substring(startIndex, s.length()));
                result.add(getIp());
                tempList.pollLast();
            }
            return;
        }
        for (int i = startIndex; i < s.length() && tempList.size()<=4; i++) {
            if (validIpParam(s.substring(startIndex, i+1))) {
                tempList.offer(s.substring(startIndex, i+1));
            } else {
                continue;
            }
            backTracking(i+1, s);
            tempList.pollLast();
        }
    }

    public boolean validIpParam(String s) {
        if (s == null || s.length()==0) {
            return false;
        }
        if (s.length()>=2 && s.charAt(0)=='0') {
            return false;
        }
        if (s.length()>3) {
            return false;
        }
        Integer num = Integer.valueOf(s);
        if (num < 0 || num > 255) {
            return false;
        }
        return true;
    }

    public String getIp() {
        StringBuilder resultStr = new StringBuilder();
        for (int i = 0; i < tempList.size(); i++) {
            if (i==tempList.size()-1) {
                resultStr.append(tempList.get(i));
            } else {
                resultStr.append(tempList.get(i)).append(".");
            }
        }
        return resultStr.toString();
    }
}

78.子集

文字讲解子集

视频讲解子集

状态:这一题的关键在于收集元素的位置,理解了回溯算法中的树形结构和理论知识,这题可以想到在for循环中收集元素即可

思路:

代码:

java 复制代码
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> tempList = new LinkedList<>();

    public List<List<Integer>> subsets(int[] nums) {
        backTracking(nums, 0);
        result.add(new ArrayList<>());
        return result;
    }

    public void backTracking(int[] nums, int index) {
        if (index>=nums.length) {
            return;
        }
        for (int i = index; i < nums.length; i++) {
            tempList.add(nums[i]);
          	//收集元素
            result.add(new ArrayList<>(tempList));
            backTracking(nums, i+1);
            tempList.pollLast();
        }
    }
}

90.子集II

文字讲解子集II

视频讲解子集II

状态:这题秒了

思路:

代码:

java 复制代码
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> tempList = new LinkedList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        result.add(tempList);
        //对数组先进行排序
        Arrays.sort(nums);
        backTracking(nums, 0);
        return result;
    }

    public void backTracking(int[] nums, int startIndex) {
        if (startIndex>=nums.length) {
            return;
        }
        for (int i = startIndex; i < nums.length; i++) {
            if (i>startIndex&&nums[i]==nums[i-1]) {
                continue;
            }
            tempList.offer(nums[i]);
            result.add(new ArrayList<>(tempList));
            backTracking(nums, i+1);
            tempList.pollLast();
        }
    }
}
相关推荐
NE_STOP30 分钟前
MyBatis-配置文件解读及MyBatis为何不用编写Mapper接口的实现类
java
后端AI实验室5 小时前
用AI写代码,我差点把漏洞发上线:血泪总结的10个教训
java·ai
CoovallyAIHub6 小时前
Moonshine:比 Whisper 快 100 倍的端侧语音识别神器,Star 6.6K!
深度学习·算法·计算机视觉
CoovallyAIHub7 小时前
速度暴涨10倍、成本暴降6倍!Mercury 2用扩散取代自回归,重新定义LLM推理速度
深度学习·算法·计算机视觉
CoovallyAIHub7 小时前
实时视觉AI智能体框架来了!Vision Agents 狂揽7K Star,延迟低至30ms,YOLO+Gemini实时联动!
算法·架构·github
CoovallyAIHub7 小时前
开源:YOLO最强对手?D-FINE目标检测与实例分割框架深度解析
人工智能·算法·github
程序员清风7 小时前
小红书二面:Spring Boot的单例模式是如何实现的?
java·后端·面试
belhomme7 小时前
(面试题)Redis实现 IP 维度滑动窗口限流实践
java·面试
CoovallyAIHub7 小时前
OpenClaw:从“19万星标”到“行业封杀”,这只“赛博龙虾”究竟触动了谁的神经?
算法·架构·github