132. Palindrome Partitioning II (动态规划)
来源:互联网 发布:jasper陈小春知乎 编辑:程序博客网 时间:2024/06/05 20:22
刷题地址
https://leetcode.com/problems/palindrome-partitioning-ii/#/description
AC
思路详解
http://blog.csdn.net/jin_kwok/article/details/51423222
- 超时代码:
class Solution {public: bool isPalindrome(string s, int sta, int en) { int i = sta; int j = en; while(i < j) { if(s[i] != s[j]) return false; i ++; j --; } return true; } int minCut(string s) { int len = s.size(); vector<vector<int>> dp(len,vector<int>(len,0)); for(int i=0;i<len;i++) { dp[i][i] = 1; for(int j = i + 1 ; j < len;j++) { if(isPalindrome(s,i,j)) dp[i][j] = 1; } } vector<int> cnt(len + 1, 0); cnt[len] = -1; cnt[len-1] = 0; for(int i=len-2; i>=0; i--) { cnt[i] = 1 + cnt[i+1]; for(int j = i+1;j < len;j++) { if(dp[i][j] == 1) { cnt[i] = min(cnt[i], 1 + cnt[j+1]); } } } return cnt[0]; }};
2.ac代码
class Solution {public: int minCut(string s) { int len = s.size(); vector<vector<int>> dp(len,vector<int>(len,0)); vector<int> cnt(len + 1, 0); cnt[len] = -1; for(int i=len-1; i>=0; i--) { cnt[i] = 1 + cnt[i+1]; for(int j = i;j < len;j++) { // s[i-j]的回文判断 if(s[i] == s[j] && (j-i<=1 ||dp[i+1][j-1] == 1) ) { dp[i][j] = 1; cnt[i] = min(cnt[i], 1 + cnt[j+1]); } } } return cnt[0]; }};
阅读全文
0 0
- 132. Palindrome Partitioning II (动态规划)
- 「动态规划」Palindrome Partitioning II
- LeetCode 132 Palindrome Partitioning II (动态规划)
- Word Break I,II, Triangle,Palindrome Partitioning 动态规划 DP
- LeetCode 之 Palindrome Partitioning II(动态规划)
- leetcode之palindrome-partitioning-ii(动态规划)
- 每日AC-LeetCode-palindrome-partitioning-ii --动态规划
- 【LeetCode】132. Palindrome Partitioning II 基于动态规划DP、C++、Java的分析及解法
- Palindrome Partitioning与动态规划
- 132. Palindrome Partitioning II
- 132. Palindrome Partitioning II
- 132. Palindrome Partitioning II
- 132. Palindrome Partitioning II
- 132. Palindrome Partitioning II
- 132. Palindrome Partitioning II
- 132. Palindrome Partitioning II
- 132. Palindrome Partitioning II
- 132. Palindrome Partitioning II
- 众数问题
- 算法设计作业15
- 洛谷 P1566 加等式
- [ACM] HDU 2025 查找最大元素
- 39级台阶问题
- 132. Palindrome Partitioning II (动态规划)
- 数位dp总结
- Java Swing事件处理——键盘事件及监听处理 KeyListener 按键测试
- 连续邮资问题
- javascript 字符串对象方法
- iOS 如何启动app使logo右上角的数字隐藏
- opencl体绘制结果有边框现象解决方案
- java并发
- 欧几里得算法及其拓展