[Leetcode]Palindrome Partitioning II
2013-06-19 11:02
218 查看
思路:动态规划
步骤:
1、计算字符串所有字串是否是回文(可以避免后面计算时重复的判断是否是回文)
状态方程:
2、DP计算最小分割数
状态方程:
步骤:
1、计算字符串所有字串是否是回文(可以避免后面计算时重复的判断是否是回文)
状态方程:
2、DP计算最小分割数
状态方程:
class Solution { public: int minCut(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<int> f(s.size()+1, 0); vector<vector<bool>> p(s.size(), vector<bool>(s.size(), false)); for(int i = s.size() - 1; i >= 0; i--) for(int j = i; j < s.size(); ++j) { if(s[i] == s[j] && (j-i < 2 || p[i+1][j-1])) p[i][j] = true; } for(int i = 1; i <= s.size(); ++i) { f[i] = INT_MAX; for(int j = 0; j < i; ++j) if(p[j][i-1]) f[i] = min(f[i], f[j] + 1); } return f[s.size()] - 1; } };
相关文章推荐
- Palindrome Partitioning II Leetcode Java
- [leetcode] Palindrome Partitioning II
- leetcode Palindrome Partitioning II DP 深搜
- [LeetCode] Palindrome Partitioning II
- leetcode132 Palindrome Partitioning II
- leetcode 132 —— Palindrome Partitioning II
- Palindrome Partitioning II Leetcode java
- LeetCode之Palindrome Partitioning II
- LeetCode132 Palindrome Partitioning II
- LeetCode 132 Palindrome Partitioning II--In C++
- [leetcode刷题系列]Palindrome Partitioning II
- LeetCode:Palindrome Partitioning II
- LeetCode-Palindrome Partitioning II
- leetcode Palindrome Partitioning II
- Leetcode--Palindrome Partitioning II
- 【LeetCode】Palindrome Partitioning II
- LeetCode Palindrome Partitioning, Palindrome Partitioning II
- Palindrome Partitioning II Leetcode Python
- 【leetcode】Palindrome Partitioning II
- 每日AC-LeetCode-palindrome-partitioning-ii --动态规划