leetcode Palindrome Partitioning II
2013-07-09 13:28
274 查看
题意:给定一个字符串s,将s进行分割,分割后每一部分都是回文串,求最少的分割次数。
思路:此题是一个DP问题,设dp[i]为i到字符串s末尾满足要求所需要的最少切割次数,j > i。当i到j为回文数时,我们判断dp[i] = min(dp[i], 1 + dp[j + 1])。
代码实现:
思路:此题是一个DP问题,设dp[i]为i到字符串s末尾满足要求所需要的最少切割次数,j > i。当i到j为回文数时,我们判断dp[i] = min(dp[i], 1 + dp[j + 1])。
代码实现:
class Solution { public: int minCut(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function int len = s.length(); int dp[len + 1]; for(int i = 0; i <= len; ++i) { dp[i] = len - i; } bool plain[len + 1][len + 1]; memset(plain, false, sizeof(plain)); for(int i = 0; i < len; ++i) { plain[i][i] = true; } for(int i = len - 1; i >= 0; --i) { dp[i] = min(dp[i], 1 + dp[i + 1]); for(int j = i + 1; j < len; ++j) { if(((j - i) == 1 || plain[i + 1][j - 1]) && s[i] == s[j]) { plain[i][j] = true; dp[i] = min(dp[i], 1 + dp[j + 1]); } } } return dp[0] - 1; } };
相关文章推荐
- [LeetCode133]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 I&II
- LeetCode – Refresh – Palindrome Partitioning II
- [LeetCode][Java] Palindrome Partitioning II
- leetcode Palindrome Partitioning II
- LeetCode OJ Palindrome Partitioning II
- leetcode-Palindrome Partitioning II
- leetcode: Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
- Palindrome Partitioning II--LeetCode
- Palindrome Partitioning II -- leetcode
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- Leetcode237: Palindrome Partitioning II
- leetcode:Palindrome Partitioning II
- leetcode - Palindrome Partitioning II