LeetCode题解 回溯(三):131 分割回文串;93 复原IP地址;78 子集

131 分割回文串 medium

给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。

返回 s 所有可能的分割方案。

示例: 输入: “aab” 输出: [ [“aa”,“b”], [“a”,“a”,“b”] ]

这道题的不同之处在于终止条件,以前都是往深处遍历时找到一个答案就返回,但这回却是找到答案还需要继续找。又因为这道题是在一个集合中寻找不同的组合,所以需要在回溯时加上起始索引位置。

那么,我们来想一下何时可以加入到结果中呢?

需要想到,如果判断子串是回文,那么就可以把子串加入到结果中,但如果不是,就需要从不是的位置开始,重新判断之后的字符串是不是。比如,abaab不是回文,但是剩下的a是。

而当起始索引值大于s的大小的时候,本轮深度递归就可以彻底退出了。

vector<vector<string>> res;
vector<string> path;
void reback(string s, int startIndex) {
    
    
    if (startIndex >= s.size()) {
    
    
        res.push_back(path);
        return;
    }

    for (int i = startIndex; i < s.size(); i++) {
    
    
        if (isPalindrome(s, startIndex, i)) {
    
    
            string temp = s.substr(startIndex, i - startIndex + 1);
            path.push_back(temp);
        } else {
    
    
            continue;
        }
        reback(s, i + 1);
        path.pop_back();
    }
    return;
}

bool isPalindrome (string &s, int startIndex, int endIndex) {
    
    
    for (int i = startIndex, j = endIndex; i < j; i++, j--) {
    
    
        if (s[i] != s[j])
            return false;
    }
    return true;
}

bool isPalindrome(string &s, int startIndex, int endIndex) {
    
    
    for (int i = startIndex, j = endIndex; i < j; i++, j--) {
    
    
        if (s[i] != s[j])
            return false;
    }
    return true;
}

93 复原IP地址 medium

这道题和上面一题有种系出同门的味道,既要做递归,又要判断结果的合理性。

首先,终止条件很好想,有效的IP地址中,只会有三个.,所以当点数达到3的时候,这次深度递归就可以结束了。

单层递归的逻辑,就是要先判断当前的“整数”是否合理,如果合理,就可以添加.,然后起始位置来到之后的两个字符(因为刚添加了点)。

而判断是否合理的函数,第一点是,当前带判断区间为空;

第二点是,如果当前的起始位置是0,但起始位置与末尾不同,就会出现例如011的地址段,这是不合理的

第三点就是正常判断,判断区间内的字符不能在0~9之外,而且区间值不能大于255.

综上所述,代码如下:

vector<string> res;
void reback(string s, int startIndex, int pointNums) {
    
    
    if (pointNums == 3) {
    
    
        if (isVaild(s, startIndex, s.size() - 1))
            res.push_back(s);
    }

    for (int i = startIndex; i < s.size(); i++) {
    
    
        if (isVaild(s, startIndex, i)) {
    
    
            s.insert(s.begin() + i + 1, '.');
            pointNums++;
            reback(s, i + 2, pointNums);
            pointNums--;
            s.erase(s.begin() + i + 1);
        }
        else break;
    }
}

bool isVaild(string s, int startIndex, int endIndex) {
    
    
    if (startIndex > endIndex) return false;
    if (s[startIndex] == '0' && startIndex != endIndex) return false;
    int sum = 0;
    for (int i = startIndex; i <= endIndex; i++) {
    
    
        if (s[i] < '0' && s[i] > '9') return false;
        sum = sum * 10 + (s[i] - '0');
        if (sum > 255) return false;
    }
    return true;
}

vector<string> restoreIpAddresses(string s) {
    
    
    if (s.size() < 4 || s.size() > 12) return {
    
    };

    reback(s, 0, 0);
    return res;
}

78 子集 medium

这道题目比前两道简单许多,甚至都不需要常规的终止条件,遇到子集,加就完事儿了,整体代码如下:

vector<vector<int>> res;
vector<int> path;
void reback(vector<int>& nums, int startIndex) {
    
    
    res.push_back(path);

    for (int i = startIndex; i < nums.size(); i++) {
    
    
        path.push_back(nums[i]);
        reback(nums, i + 1);
        path.pop_back();
    }
}

vector<vector<int>> subsets(vector<int>& nums) {
    
    
    reback(nums, 0);

    return res;
}

猜你喜欢

转载自blog.csdn.net/qq_41205665/article/details/128667507