算法学习day28回溯part04-93、78、90

发布时间 2023-05-28 14:48:58作者: 坤坤无敌
package LeetCode.backtrackpart04;

import java.util.ArrayList;
import java.util.List;

/**
 * 93. 复原 IP 地址
 * 有效 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 '.' 分隔。
 * 例如:"0.1.2.201" 和 "192.168.1.1" 是 有效 IP 地址,
 * 但是 "0.011.255.245"、"192.168.1.312" 和 "192.168@1.1" 是 无效 IP 地址。
 * 给定一个只包含数字的字符串 s ,用以表示一个 IP 地址,返回所有可能的有效 IP 地址,
 * 这些地址可以通过在 s 中插入'.' 来形成。你 不能重新排序或删除 s 中的任何数字。你可以按 任何 顺序返回答案。
 * 示例:
 * 输入:s = "25525511135"
 * 输出:["255.255.11.135","255.255.111.35"]
 * */
/**
 * 太难了。要二刷
 *
 * */

public class RestoreIPAddresses_93 {
    static List<String> result = new ArrayList<>();
    public static void main(String[] args) {
        String str = "25525511135";
        result = restoreIpAddresses(str);
        System.out.println(result);

    }



    public static List<String> restoreIpAddresses(String s) {
        if (s.length() > 12) return result; // 算是剪枝了
        backTrack(s, 0, 0);
        return result;
    }

    // startIndex: 搜索的起始位置, pointNum:添加逗点的数量
    public static void backTrack(String s, int startIndex, int pointNum) {
        if (pointNum == 3) {// 逗点数量为3时,分隔结束
            // 判断第四段⼦字符串是否合法,如果合法就放进result中
            if (isValid(s,startIndex,s.length()-1)) {
                result.add(s);
            }
            return;
        }
        for (int i = startIndex; i < s.length(); i++) {
            if (isValid(s, startIndex, i)) {
                s = s.substring(0, i + 1) + "." + s.substring(i + 1);    //在str的后⾯插⼊⼀个逗点
                pointNum++;
                backTrack(s, i + 2, pointNum);// 插⼊逗点之后下⼀个⼦串的起始位置为i+2
                pointNum--;// 回溯
                s = s.substring(0, i + 1) + s.substring(i + 2);// 回溯删掉逗点
            } else {
                break;
            }
        }
    }

    // 判断字符串s在左闭⼜闭区间[start, end]所组成的数字是否合法
    public static Boolean isValid(String s, int start, int end) {
        if (start > end) {
            return false;
        }
        if (s.charAt(start) == '0' && start != end) { // 0开头的数字不合法
            return false;
        }
        int num = 0;
        for (int i = start; i <= end; i++) {
            if (s.charAt(i) > '9' || s.charAt(i) < '0') { // 遇到⾮数字字符不合法
                return false;
            }
            num = num * 10 + (s.charAt(i) - '0');
            if (num > 255) { // 如果⼤于255了不合法
                return false;
            }
        }
        return true;
    }
}
package LeetCode.backtrackpart04;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 78. 子集
 * 给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
 * 解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
 * 示例:
 * 输入:nums = [1,2,3]
 * 输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
 * */
public class Subsets_78 {
    static List<List<Integer>> result = new ArrayList<>();
    static LinkedList<Integer> path  = new LinkedList<>();
    public static void main(String[] args) {
        int [] nums = {1,2,3};
        result = subsets(nums);
        System.out.println(result);
    }
    public static List<List<Integer>> subsets(int [] nums){
        subsetHelper(nums,0);
        return result;
    }

    public static void subsetHelper(int [] nums,int startIndex){
        result.add(new ArrayList<>(path));
        //终止条件
        if (startIndex >= nums.length){
            return;
        }
        for (int i = startIndex; i < nums.length; i++) {
            path.add(nums[i]);
            subsetHelper(nums,i+1);
            path.removeLast();
        }
    }
}
package LeetCode.backtrackpart04;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * 90.子集 II
 *给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。
 * 解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。
 * 示例:
 * 输入:nums = [1,2,2]
 * 输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]
 * */
public class SubsetsII_90 {
    static List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
    static LinkedList<Integer> path = new LinkedList<>();// 用来存放符合条件结果
    static boolean[] used;

    public static void main(String[] args) {
        int [] nums = {1,2,2};
        result = subsetsWithDup(nums);
        System.out.println(result);
    }

    public static List<List<Integer>> subsetsWithDup(int[] nums) {
        if (nums.length == 0){
            result.add(path);
            return result;
        }
        Arrays.sort(nums);
        used = new boolean[nums.length];
        subsetsWithDupHelper(nums, 0);
        return result;
    }

    public static void subsetsWithDupHelper(int[] nums, int startIndex){
        result.add(new ArrayList<>(path));
        if (startIndex >= nums.length){
            return;
        }
        for (int i = startIndex; i < nums.length; i++){
            if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]){
                continue;
            }
            path.add(nums[i]);
            used[i] = true;
            subsetsWithDupHelper(nums, i + 1);
            path.removeLast();
            used[i] = false;
        }
    }
}