[LeetCode]Palindrome Partitioning II
2014-03-12 22:10
375 查看
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
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: int minCut(string s) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int len = s.size(); int* dp = new int[len+1]; for(int i=len; i>=0; i--) dp[i] = len-i; bool** matrix = new bool*[len]; for(int i=0; i<len; i++) { matrix[i] = new bool[len]; memset(matrix[i], false, sizeof(bool)*len); } for(int i=len-1; i>=0; i--) for(int j=i; j<len; j++) { if(s[i] == s[j] && (j-i<2 || matrix[i+1][j-1])) { matrix[i][j] = true; dp[i] = min(dp[i], dp[j+1]+1); } } return dp[0]-1; } };
相关文章推荐
- [LeetCode]—Palindrome Partitioning II 回文分割,求最小分割数
- leetcode——Palindrome Partitioning II
- LeetCode 132. 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 (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
- Palindrome Partitioning II Leetcode java
- [LeetCode] Palindrome Partitioning II
- LeetCode之Palindrome Partitioning II
- LeetCode132 Palindrome Partitioning II