LeetCode-131. Palindrome Partitioning

网友投稿 482 2022-11-09

LeetCode-131. Palindrome Partitioning

LeetCode-131. Palindrome Partitioning

Given a string s, partition s such that every substring of the partition is a palindrome.

Return all possible palindrome partitioning of s.

Example:

Input: "aab"Output:[ ["aa","b"], ["a","a","b"]]

题解:

低效率dfs

class Solution {public: bool isPalindrome (string s) { int n = s.length(); for (int i = 0; i < n / 2; i++) { if (s[i] != s[n - i - 1]) { return false; } } return true; } void dfs(string &s, vector> &res, vector &tmp, int &n, int k) { if (k == n) { res.push_back(tmp); } else if (k < n) { for (int i = 1; i < n - k + 1; i++) { string idx = s.substr(k, i); if (isPalindrome(idx) == true) { tmp.push_back(idx); dfs(s, res, tmp, n, k + i); tmp.pop_back(); } } } } vector> partition(string s) { int n = s.length(); vector> res; vector tmp; dfs(s, res, tmp, n, 0); return res; }};

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:LeetCode-127. Word Ladder
下一篇:LeetCode-132. Palindrome Partitioning II
相关文章

 发表评论

暂时没有评论,来抢沙发吧~