1. 分割回文串
131. 分割回文串 https://leetcode.cn/problems/palindrome-partitioning/
https://leetcode.cn/problems/palindrome-partitioning/
给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。
回文串 是正着读和反着读都一样的字符串。
示例 1:
输入:s = "aab"
输出:[["a","a","b"],["aa","b"]]示例 2:
输入:s = "a"
输出:[["a"]]解题思路
要做的流程就是以每一个字符为启示,每一个字符为终止,判断是否为回文数,所以使用回溯。
代码
class Solution {
    List<List<String>> res = new ArrayList<>();
    Deque<String> list = new LinkedList<>();
    public List<List<String>> partition(String s) {
        helper(s, 0);
        return res;
    }
    public void helper(String s, int i) {
        if (i >= s.length()) {
            res.add(new ArrayList(list));
            return;
        }
        for (int j = i; j < s.length(); j++) {
            if (isPalindrome(s, i, j )) {
                String str = s.substring(i, j + 1);
                list.addLast(str);
            } else {
                continue;
            }
            helper(s, j + 1);
            list.removeLast();
        }
    }
    public boolean isPalindrome(String str, int left, int right) {
        while (left < right) {
            if (str.charAt(left++) != str.charAt(right--)) {
                return false;
            }
        }
        return true;
    }
}2. 复原IP地址
93. 复原 IP 地址 https://leetcode.cn/problems/restore-ip-addresses/
https://leetcode.cn/problems/restore-ip-addresses/
有效 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 中的任何数字。你可以按 任何 顺序返回答案。
示例 1:
输入:s = "25525511135"
输出:["255.255.11.135","255.255.111.35"]示例 2:
输入:s = "0000"
输出:["0.0.0.0"]示例 3:
输入:s = "101023"
输出:["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]解题思路
需要找到一个字符串的组合方式,这是一个回溯问题。
- 终止条件:当开始位置大于字符串长度的时候,加入返回值。
- 参数:一个字符串,一个起始索引
- 回溯逻辑:循环遍历起始索引开始的位置,对每一个切分出来的数字判断,如果不满足就break掉(剪枝),满足就进入下一层递归。
代码
class Solution {
    List<String> res = new ArrayList<>();
    public List<String> restoreIpAddresses(String s) {
        if (s.length() > 12)
            return res;
        backtracking(s, 0, 0);
        return res;
    }
    public void backtracking(String s, int startIndex, int pointCount) {
        if (pointCount == 3) {
            if (isIpParam(s, startIndex, s.length() - 1))
                res.add(s);
            return;
        }
        for (int i = startIndex; i < s.length(); i++) {
            if (isIpParam(s, startIndex, i)) {
                s = s.substring(0, i + 1) + "." + s.substring(i + 1, s.length());
                backtracking(s, i + 2, pointCount + 1);
                s = s.substring(0, i + 1) + s.substring(i + 2, s.length());
            } else {
                break;
            }
        }
    }
    public boolean isIpParam(String s, int startIndex, int endIndex) {
        if (startIndex > endIndex)
            return false;
        if (s.charAt(startIndex) == '0' && startIndex != endIndex)
            return false;
        int num = 0;
        while (startIndex <= endIndex) {
            num = num * 10 + (s.charAt(startIndex) - '0');
            if (num > 255)
                return false;
            startIndex++;
        }
        return true;
    }
}3. 子集
78. 子集 https://leetcode.cn/problems/subsets/
https://leetcode.cn/problems/subsets/
给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
示例 1:
输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]示例 2:
输入:nums = [0]
输出:[[],[0]]解题思路
要在不重复的集合内,找到所有的子集,所有的子集也就是要记录所有的回溯算法的路径。回溯算法本身就是不重复路径的。所以在回溯算法一开始就要进行result的add操作。每执行一次函数就add一次。本身回溯算法的第一次递归中,就会以每个节点为起点进行操作,在以每个节点为起点的操作的下个迭代中,又是以每一个节点为起点的迭代。
代码
class Solution {
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> list = new LinkedList<>();
    public List<List<Integer>> subsets(int[] nums) {
        backTrack(nums, 0);
        return res;
    }
    public void backTrack(int[] nums, int index) {
        res.add(new ArrayList(list));
        if (index == nums.length)
            return;
        for (int i = index; i < nums.length; i++) {
            list.add(nums[i]);
            backTrack(nums, i + 1);
            list.removeLast();
        }
    }
}

















