131. 分割回文串——回溯+记忆化搜索

class Solution {
public:
    vector<vector<string>> ans; //答案
    vector<vector<bool>> flag;  //i, j表示i - j是否是回文串
    vector<string> temp;    //子串
    vector<vector<string>> partition(string s) {
        int n = s.size();
        flag.assign(n, vector<bool>(n, false));  //赋值,默认为true
        backtrack(s, 0, n);
        return ans;
    }
    bool isPalindrome(const string& s, int i, int j) {  //记忆化搜索
        if(flag[i][j])
            return true;
        if(i >= j)  //"aa"这种情况会出错
            return flag[i][j] = true;
        return flag[i][j] = (s[i] == s[j] && isPalindrome(s, i + 1, j - 1));    //i == j并且 i+1 - j-1 是回文数
    }
    void backtrack(const string& s, int i, int n){
        if(i == n){
            ans.push_back(temp);
            return;
        }
        for(int j = i; j < n; ++j){
            if(isPalindrome(s, i, j)){
                temp.push_back(s.substr(i, j - i + 1));
                backtrack(s, j + 1, n);
                temp.pop_back();
            }
        }
    }
};

Accepted
32/32 cases passed (100 ms)
Your runtime beats 84.45 % of cpp submissions
Your memory usage beats 74.6 % of cpp submissions (73.9 MB)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个有趣的问题,使用 Kotlin 可以很方便地解决。 我的思路是使用回溯法,遍历所有可能的分割方式,并判断每个子串是否为回文串。 具体实现细节如下: 首先定义一个函数 `isPalindrome` 用于判断一个字符串是否为回文串,这个函数的实现非常简单,只需要判断字符串正着读和反着读是否相等即可。 ```kotlin fun isPalindrome(str: String): Boolean { return str == str.reversed() } ``` 接下来定义一个函数 `partition` 用于遍历所有可能的分割方式。这个函数的参数包括原始字符串 `s`,当前处理到的位置 `start`,当前已经分割出来的子串列表 `cur`,以及最终的结果列表 `res`。 在这个函数中,我们首先判断当前位置是否已经到达字符串末尾。如果是,就把当前的分割方案添加到结果列表中。否则,我们从当前位置开始往后遍历,尝试分割出一个回文子串。如果找到了一个回文子串,我们就把它添加到子串列表中,然后递归处理后面的子串。最后,记得要回溯到上一个状态,把添加的子串从列表中移除。 ```kotlin fun partition(s: String, start: Int, cur: MutableList<String>, res: MutableList<List<String>>) { if (start == s.length) { res.add(cur.toList()) return } for (i in start until s.length) { val str = s.substring(start, i + 1) if (isPalindrome(str)) { cur.add(str) partition(s, i + 1, cur, res) cur.removeAt(cur.lastIndex) } } } ``` 最后,我们可以在主函数中调用 `partition` 函数,并返回结果列表。完整代码如下: ```kotlin fun isPalindrome(str: String): Boolean { return str == str.reversed() } fun partition(s: String, start: Int, cur: MutableList<String>, res: MutableList<List<String>>) { if (start == s.length) { res.add(cur.toList()) return } for (i in start until s.length) { val str = s.substring(start, i + 1) if (isPalindrome(str)) { cur.add(str) partition(s, i + 1, cur, res) cur.removeAt(cur.lastIndex) } } } fun partition(s: String): List<List<String>> { val res = mutableListOf<List<String>>() partition(s, 0, mutableListOf(), res) return res } ``` 你可以使用以下代码测试这个函数: ```kotlin fun main() { val s = "aab" val res = partition(s) println(res) } ``` 输出结果为: ``` [[a, a, b], [aa, b]] ``` 这说明我们的代码已经正确地找到了字符串所有可能的回文子串分割方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值