Day28 回溯算法 part04

Day28 回溯算法 part04

93.复原IP地址

我的思路:

在分割回文串代码的基础上改写,需要考虑多种情况

不合法的IP地址part:长度>1同时首数字为0 || 长度=0 || 长度>3 || >255

将各个part结合起来添加 "." 的时候,需要分前三part和最后一个,因此回溯完删除的范围也需要分别考虑

java 复制代码
class Solution {
    public List<String> res = new ArrayList();
    public StringBuffer path = new StringBuffer();
    public List<String> restoreIpAddresses(String s) {
        if(s == null || s.length() == 0) {
            return new ArrayList();
        }
        backtracing(s, 0, 0);
        return res;
    }
    public void backtracing(String s, int startIndex, int partNum) {
        if(partNum == 4) {
            if(startIndex == s.length()) {
                StringBuffer sb = new StringBuffer(path);
                res.add(sb.toString());
            }
            return;
        }
        for(int i = startIndex; i < s.length(); i++) {
            String part = s.substring(startIndex, i + 1);
            if(isValid(part)) {
                if(partNum < 3) {
                    path.append(part);
                    path.append('.');
                    backtracing(s, i + 1 , partNum + 1);
                    path.delete(path.length() - part.length() - 1, path.length());
                }
                else {
                    path.append(part);
                    backtracing(s, i + 1, partNum + 1);
                    path.delete(path.length() - part.length(), path.length());
                }  
            }
            else {
                break;
            }
        }
    }
    public boolean isValid(String s) {
        if(s.length() == 0 || s.length() > 3 || (s.charAt(0) == '0' && s.length() > 1) 
        || Integer.parseInt(s) > 255) {
            return false;
        }
        return true;
    }
}

78.子集

我的思路:

不需要考虑返回情况,将path全部加到result集合中

解答:

java 复制代码
class Solution {
    public List<Integer> path = new ArrayList();
    public List<List<Integer>> res = new ArrayList();
    public List<List<Integer>> subsets(int[] nums) {
        Arrays.sort(nums);
        backtracing(nums, 0);
        return res;
    }
    public void backtracing(int[] nums, int startIndex) {
        res.add(new ArrayList(path));
        for(int i = startIndex; i < nums.length; i++) {
            path.add(nums[i]);
            backtracing(nums, i+1);
            path.remove(path.size() - 1);
        }
    }
}

90.子集II

我的思路:

在上一题的基础上,判断要添加到path,在result里面是不是已经存在

另外模仿组合的做法进行了剪枝

解答:

java 复制代码
class Solution {
    public List<Integer> path = new ArrayList();
    public List<List<Integer>> res = new ArrayList();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        backtracing(nums, 0);
        return res;
    }
    public void backtracing(int[] nums, int startIndex) {
        if(hasNums(path, res)) {
            res.add(new ArrayList(path));
        }
        for(int i = startIndex; i < nums.length; i++) {
            if(i > startIndex && nums[i] == nums[i-1]) {
                continue;
            }
            path.add(nums[i]);
            backtracing(nums, i+1);
            path.remove(path.size() - 1);
        }
    }
    public boolean hasNums(List<Integer> path, List<List<Integer>> res) {
        for(List l : res) {
            if(path.equals(l)){
                return false;
            }
        }
        return true;
    }
}
相关推荐
Wyc724095 分钟前
Maven
java·数据库·maven
军训猫猫头7 分钟前
1.如何对多个控件进行高效的绑定 C#例子 WPF例子
开发语言·算法·c#·.net
程序猿小D8 分钟前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的电影小说网站管理系统,推荐!
java·数据库·mysql·spring·毕业设计·ssm框架·电影小说网站
success20 分钟前
【爆刷力扣-数组】二分查找 及 衍生题型
算法
Orlando cron1 小时前
数据结构入门:链表
数据结构·算法·链表
木头没有瓜1 小时前
idea离线安装插件
java·ide·intellij-idea
llwszx2 小时前
Spring中DelayQueue深度解析:从原理到实战(附结构图解析)
java·后端·spring·delayqueue·延迟任务
牛客企业服务2 小时前
2025年AI面试推荐榜单,数字化招聘转型优选
人工智能·python·算法·面试·职场和发展·金融·求职招聘
述雾学java2 小时前
Spring Cloud Feign 整合 Sentinel 实现服务降级与熔断保护
java·spring cloud·sentinel
保持学习ing2 小时前
苍穹外卖day3--公共字段填充+新增菜品
java·阿里云·实战·springboot·前后端·外卖项目·阿里云文件存储