【LeetCode】Palindrome Partitioning II
2014-05-07 23:20
381 查看
题目描述:
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s =
Return
be produced using 1 cut.
但是这段程序会TLE,作为一个初学者实在想不到更好的办法了( ̄_ ̄|||) ,于是去翻了下讨论区,发现新的解法:
用的动态规划解法。公式为dp[j+1]=min(dp[j+1], dp[i]+1) if (s[i] == s[j] && s[i+1] : [j-1]为回文),判定i:j是不是回文只需要s[i]==s[j]并且s[i+1] : [j-1]是回文就可以了。
dp[ i ]为到 i-1 为止的最小切割数。p控制奇偶数。若判定s[a:b]为回文,再切一刀即dp[a]+1,与dp[b+1]取最小值赋值给dp[b+1]。最后结束的时候多切一刀,因此返回dp
-1
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s =
"aab",
Return
1since the palindrome partitioning
["aa","b"]could
be produced using 1 cut.
class Solution { public: vector<vector<int>> pal; vector<vector<string>> res; int min; int minCut(string s) { int path = 0; min = s.length(); for (int i = 0; i < s.size(); i++) { vector<int> line; pal.push_back(line); } for (int i = 0; i < s.length(); i++) ispal(s, 0, i); for (int i = 1; i < s.length(); i++) { ispal(s, i, s.length() - 1); } cut(0, path, s); return min; } void cut(int start, int path, string &s) { if (start>s.length() - 1) { min = (path - 1)<min ? (path - 1) : min; return; } for (int i = 0; i < pal[start].size(); i++) { int temp = path; temp++; cut(pal[start][i], temp, s); } } bool ispal(string &s, int start, int end) { if (start > end) return true; if (ispal(s, start + 1, end - 1) && s[start] == s[end]) { pal[start].push_back(end + 1); return true; } else return false; } };
但是这段程序会TLE,作为一个初学者实在想不到更好的办法了( ̄_ ̄|||) ,于是去翻了下讨论区,发现新的解法:
class Solution { public: int minCut(string s) { int N = s.size(); vector<int> dp(N+1,numeric_limits<int>::max()); dp[0] = 0; for(int i=0;i<N;i++) for(int p=0;p<=1;p++) for(int l=0;l<=N;l++) { int a=i-l, b = i+l+p; if( a < 0 || b >= N || s[a]!=s[b] ) break; dp[b+1] = min(dp[b+1],dp[a]+1); } return dp -1; } };
用的动态规划解法。公式为dp[j+1]=min(dp[j+1], dp[i]+1) if (s[i] == s[j] && s[i+1] : [j-1]为回文),判定i:j是不是回文只需要s[i]==s[j]并且s[i+1] : [j-1]是回文就可以了。
dp[ i ]为到 i-1 为止的最小切割数。p控制奇偶数。若判定s[a:b]为回文,再切一刀即dp[a]+1,与dp[b+1]取最小值赋值给dp[b+1]。最后结束的时候多切一刀,因此返回dp
-1
相关文章推荐
- Leetcode -- Palindrome Partitioning II
- [Leetcode] Palindrome Partitioning II (Java)
- LeetCode Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning && Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- [leetcode-132]Palindrome Partitioning II(java)
- Leetcode-Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- [LeetCode]—Palindrome Partitioning II 回文分割,求最小分割数
- leetcode——Palindrome Partitioning II
- LeetCode 132. Palindrome Partitioning II
- [Leetcode] Palindrome Partitioning II
- [Leetcode] Palindrome Partitioning II
- LEETCODE: Palindrome Partitioning II
- Leetcode 132. Palindrome Partitioning II
- leetcode 日经贴,Cpp code -Palindrome Partitioning II
- Leetcode Palindrome Partitioning II
- Leetcode: Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning II
- leetcode132 Palindrome Partitioning II