• 006. 分割回文串


    1.题目链接:

    131. 分割回文串

    2.解题思路:

    2.1.题目要求:

    给一个字符串 s ,要求把 s 分割成一些子串,并使每个子串都是 回文串。

    回文串的概念:正反顺序都一样的字符串。

    举例:

    输入:s = "aab"

    输出:[["a","a","b"],["aa","b"]]

    2.2.思路:

    for+递归构成 n叉树,用于查找每一份字串组合,再补充 判断是不是回文串的逻辑,以及终止条件的确认

    n叉树如下:

    2.3.回溯三部曲:

    2.3.1.确定回溯函数参数

    path存储一条分割方案的字符串,result是字符串结果集。

    参数默认输入 s ,startIndex 用于避免重复切割和切割过程中字串范围确定。

    1. vector> result;
    2. vector path; // 放已经回文的子串
    3. void backtracking (const string& s, int startIndex) {

    2.3.2.确定终止条件

    当切割完最后一个字符串字符的时候,说明已经找到了一组分割方案了,返回到结果集里。

    那什么是作为切割完最后一个字符的判断条件?

    startIndex >= s.size( ) ,因为 startIndex 在单层遍历逻辑里,记录每次递归的起始位置,到最下面一层的递归时,startIndex 默认是等于 s.size( ) 就停止了的,但大于的情况是怎么产生的?我的理解是 xxxxxx

    1. void backtracking (const string& s, int startIndex) {
    2. // 如果起始位置已经大于s的大小,说明已经找到了一组分割方案了
    3. if (startIndex >= s.size()) {
    4. result.push_back(path);
    5. return;
    6. }
    7. }

    2.3.3.确定单层遍历逻辑

    这里搜集不同的分割方案,以及判断当下递归的for搜集的每一段字符串是不是符合回文串的定义,不符合就跳过并继续循环。

    判断回文子串的范围是如何确定的?

    因为每一次递归,搜集指针 i 都从 startIndex 的位置出发(startIndex每次递归都会更新)然后 i 向后遍历,直到末尾,在这个情况下 不断更新的 i 和 在非递归的情况下不动的 startIndex 的位置形成的范围,就可以不断判断是不是回文串了,是就搜集,不是就跳过

    1. for (int i = startIndex; i < s.size(); i++) {
    2. if (isPalindrome(s, startIndex, i)) { // 是回文子串
    3. // 获取[startIndex,i]在s中的子串
    4. string str = s.substr(startIndex, i - startIndex + 1);
    5. path.push_back(str);
    6. } else { // 如果不是则直接跳过
    7. continue;
    8. }
    9. backtracking(s, i + 1); // 寻找i+1为起始位置的子串
    10. path.pop_back(); // 回溯过程,弹出本次已经填在的子串
    11. }

     

    判断回文子串的函数 isPalindrome 用的是双指针法

    代码如下:

    1. bool isPalindrome(const string& s, int start, int end) {
    2. for (int i = start, j = end; i < j; i++, j--) {
    3. if (s[i] != s[j]) {
    4. return false;
    5. }
    6. }
    7. return true;
    8. }

     

    2.4.总代码:

    1. class Solution {
    2. private:
    3. vector> result;
    4. vector path; // 放已经回文的子串
    5. void backtracking (const string& s, int startIndex) {
    6. // 如果起始位置已经大于s的大小,说明已经找到了一组分割方案了
    7. if (startIndex >= s.size()) {
    8. result.push_back(path);
    9. return;
    10. }
    11. for (int i = startIndex; i < s.size(); i++) {
    12. if (isPalindrome(s, startIndex, i)) { // 是回文子串
    13. // 获取[startIndex,i]在s中的子串
    14. string str = s.substr(startIndex, i - startIndex + 1);
    15. path.push_back(str);
    16. } else { // 不是回文,跳过
    17. continue;
    18. }
    19. backtracking(s, i + 1); // 寻找i+1为起始位置的子串
    20. path.pop_back(); // 回溯过程,弹出本次已经填在的子串
    21. }
    22. }
    23. bool isPalindrome(const string& s, int start, int end) {
    24. for (int i = start, j = end; i < j; i++, j--) {
    25. if (s[i] != s[j]) {
    26. return false;
    27. }
    28. }
    29. return true;
    30. }
    31. public:
    32. vector> partition(string s) {
    33. result.clear();
    34. path.clear();
    35. backtracking(s, 0);
    36. return result;
    37. }
    38. };

    3.记录:

    中文感觉没精神写,写的没气力,果然休息会儿,晚上写就好的多的多了,依情况而定,不能强来。

  • 相关阅读:
    【蜂鸟E203内核解析】Chap.2 E203内核中指令执行的过程-为什么E203是两级流水线?
    如何统计前端项目有多少行代码
    C51串口通信(蓝牙)
    TCP和UDP的基本认识
    Oracle中两张表具有相同结构,如何将一张表内容全部插入到另一个表中
    供给侧结构性改革语境应对世界市场 国稻种芯百团计划行动
    vue3+element-plus 表单校验和循环form表单校验
    半导体分立器件动态测试参数有哪些?纳米软件半导体测试厂商如何助力测试?
    暑期JAVA学习(39)UDP通信
    ant design vue对话框关闭数据清空
  • 原文地址:https://blog.csdn.net/qq_70280303/article/details/128129644