【代码随想录训练营】Day28-回溯算法

代码随想录训练营 Day28

今日任务

93.复原IP地址
78.子集
90.子集Ⅱ
语言:Java

93. 复原IP地址

链接:https://leetcode.cn/problems/restore-ip-addresses/

class Solution {
    List<String> result;
    List<String> path;
    public void backTracking(String s, int startIdx){
        if(path.size() == 4 && startIdx >= s.length()){
            String temp = "";
            for(int i = 0; i < 4; i++){
                if(i != 3){
                    temp += path.get(i);
                    temp += ".";
                }
                else{
                    temp += path.get(i);
                }
            }
            result.add(new String(temp));
            return;
        }
        //判断下path的个数,剪枝操作
        for(int i = startIdx; i < s.length() && path.size() <= 4; i++){
            if(!isValid(s, startIdx, i)){
                continue;
            }
            String temp = s.substring(startIdx, i + 1);
            path.add(temp);
            backTracking(s, i + 1);
            path.remove(path.size() - 1); //这里要确保移除的是最后一个元素,不要用元素值remove
        }
    }
    public boolean isValid(String s, int startIdx, int endIdx){
        String temp = s.substring(startIdx, endIdx + 1);
        //leading 0
        if(s.charAt(startIdx) == '0' && temp.length() >= 2){
            return false;
        }
        if(temp.length() > 3 || Integer.parseInt(temp) > 255){
            return false;
        }
        return true;
    }
    public List<String> restoreIpAddresses(String s) {
        result = new ArrayList<String>();
        path = new ArrayList<String>();
        backTracking(s, 0);
        return result;
    }
}

78. 子集

链接:https://leetcode.cn/problems/subsets/

class Solution {
    List<List<Integer>> result;
    List<Integer> subset;
    public void backTracking(int[] nums, int startIdx){
        //终止条件可以不加,for里有控制,且我们要遍历到最终
        if(startIdx >= nums.length){
            return;
        }
        for(int i = startIdx; i < nums.length; i++){
            subset.add(nums[i]);
            result.add(new ArrayList(subset));
            backTracking(nums, i + 1);
            subset.remove(subset.size() - 1);
        }
    }
    public List<List<Integer>> subsets(int[] nums) {
        result = new ArrayList<List<Integer>>();
        subset = new ArrayList<Integer>();
        result.add(subset); //空集
        backTracking(nums, 0);
        return result;
    }
}

90. 子集Ⅱ

链接:https://leetcode.cn/problems/subsets-ii/

class Solution {
    List<List<Integer>> result;
    List<Integer> subset;
    public void backTracking(int[] nums, int startIdx){
        result.add(new ArrayList(subset)); //这里add也可以把空集加进去
        for(int i = startIdx; i < nums.length; i++){
            if(i > startIdx && nums[i] == nums[i - 1]){
                continue;
            }
            subset.add(nums[i]);
            backTracking(nums, i + 1);
            subset.remove(subset.size() - 1);
        }
    }
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        result = new ArrayList<List<Integer>>();
        subset = new ArrayList<Integer>();
        Arrays.sort(nums);
        backTracking(nums, 0);
        return result;
    }
}

你可能感兴趣的:(代码随想录训练营,leetcode,java)