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

文章讲述了使用回溯算法解决复原IP地址和子集问题的方法,包括代码实现和关键步骤,如IP参数的有效性检查和子集的收集。
摘要由CSDN通过智能技术生成

93.复原IP地址

文字讲解复原IP地址

视频讲解复原IP地址

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

思路:

代码:

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循环中收集元素即可

思路:

代码:

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

状态:这题秒了

思路:

代码:

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();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@晴天_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值