leetcode Palindrome Partitioning
来源:互联网 发布:淘宝小积分抽大奖 编辑:程序博客网 时间:2024/06/15 10:27
Description:
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab"
,
Return
[ ["aa","b"], ["a","a","b"]]
Solution:
1.DFS
在每一步都可以判断中间结果是否为合法结果,用回溯法。 一个长度为n的字符串,有 n -1 个地方可以隔断,每个地方都有两个情况,因此复杂度为O(2^(n-1)
class Solution {public: vector<vector<string>> partition(string s) { vector<vector<string>> result; vector<string> path; DFS(s, result, path, 0); return result; } void DFS(string &s, vector<vector<string>> &result, vector<string> &path, int start) { if(start == s.size()) { result.push_back(path); return; } for (int i = start; i < s.size(); i++) { if(isPalindrome(s, start, i)) { path.push_back(s.substr(start, i-start+1)); DFS(s,result,path,i+1); path.pop_back(); } } } bool isPalindrome(string &s, int start, int end) { while (start < end && s[start] == s[end]) { start++; end--; } return start >= end; }};
2.DP
res(i)表示s[0...i-1]的所有分解方式
isPalin(i,j)表示s[i...j]是否为回文串
isPalin(i,j) = true if i==j or (s[i] == s[j] and isPalin(i + 1, j - 1)) or (s[i] == s[j] and i + 1 == j)
res(i) = res(j) + s[j...i-1] if isPalin(j, i-1)
class Solution {public: vector<vector<string>> partition(string s) { int size = s.size(); vector<vector<vector<string>>> result(size+1); result[0].push_back(vector<string>(0)); vector<vector<bool>> isPalin(size + 1, vector<bool>(size + 1, false)); for (int i = 0; i < size; i++) { for (int j = 0; j <= i; j++) { if (s[i] == s[j] && (i - j < 2 || isPalin[j + 1][i - 1])) { isPalin[j][i] = true; for (int k = 0; k < result[j].size(); k++) { vector<string> prefix = result[j][k]; prefix.push_back(s.substr(j,i-j+1)); result[i+1].push_back(prefix);//re(i) = re(j) + s[j...i-1] if isPalin(j, i-1) } } } } return result[size]; }};
阅读全文
0 0
- 【leetcode】Palindrome Partitioning && Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning && Palindrome Partitioning II
- LeetCode Palindrome Partitioning I&&Palindrome Partitioning II
- LeetCode Palindrome Partitioning, Palindrome Partitioning II
- [Leetcode][python]Palindrome Partitioning/Palindrome Partitioning II
- Leetcode:Palindrome Partitioning & Palindrome Partitioning II
- LeetCode: Palindrome Partitioning
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode 122: Palindrome Partitioning
- leetcode:Palindrome Partitioning II
- leetcode:Palindrome Partitioning
- 【leetcode】Palindrome Partitioning
- 【leetcode】Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- Leetcode - Palindrome Partitioning
- LeetCode之Palindrome Partitioning
- [LeetCode]Palindrome Partitioning
- JavaWeb学习笔记-XML-2
- js日期格式化精髓总结
- 程序员读《三体》后看到了什么?
- 清风遥寄
- 【LeetCode】419.Battleships in a Board(Medium)解题报告
- leetcode Palindrome Partitioning
- Redis Cluster集群的搭建与实践
- leetcode-month3-week10
- toString()方法对数字与关键字parseInt()方法对带有数字的字符串中数字进制之间转换-学习笔记
- java String 中 startsWith() 对字符串开头进行过滤 endsWith()对字符串结尾进行过滤
- LeetCode 279. Perfect Squares--动态规划
- 添加自定义vue全局方法,同时给自定义的方法 传递component调用其方法
- 计算一个整数各个位的和,直到和<10输出
- Linux下开发-守护进程(daemon)