132. Palindrome Partitioning II
来源:互联网 发布:socket客户端接收数据 编辑:程序博客网 时间:2024/05/22 12:57
这道题双重DP
* 方法1
参考:喜刷刷
class Solution { public: int minCut(string s) { int n = s.size(); if(n<=1) return 0; vector<vector<bool>> isPal(n, vector<bool>(n, false)); for(int i=n-1; i>=0; i--) { for(int j=i; j<n; j++) { if((i+1>j-1 || isPal[i+1][j-1]) && s[i]==s[j]) isPal[i][j] = true; } } vector<int> dp(n+1,INT_MAX); dp[0] = -1; for(int i=1; i<=n; i++) { for(int j=i-1; j>=0; j--) { if(isPal[j][i-1]) { dp[i] = min(dp[i], dp[j]+1); } } } return dp[n]; } };
- 方法二
参考水中的鱼
class Solution { public: int minCut(string s) { int len = s.size(); int D[len+1]; bool P[len][len]; for(int i = 0; i <= len; i++) D[i] = len-i; for(int i = 0; i < len; i++) for(int j = 0; j < len; j++) P[i][j] = false; for(int i = len-1; i >= 0; i--) for(int j = i; j < len; j++) { if(s[i] == s[j] && (j-i < 2 || P[i+1][j-1])) { P[i][j] = true; D[i] = min(D[i], D[j+1]+1); } } return D[0]-1; } };
0 0
- 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
- 132. Palindrome Partitioning II
- 132. Palindrome Partitioning II
- 132. Palindrome Partitioning II
- 132. Palindrome Partitioning II
- 132. Palindrome Partitioning II
- 132. Palindrome Partitioning II
- 131. Palindrome Partitioning && 132. Palindrome Partitioning II
- LeetCode 132. Palindrome Partitioning II
- [LeetCode]132.Palindrome Partitioning II
- 基于visual c++之windows核心编程代码分析(16)使用邮槽进行进程通信
- Presto 来自Facebook的开源分布式查询引擎
- 《程序员的自我修养》学习笔记之温故而知新
- C程序设计语言(第二版)-读书笔记
- 教你复杂的C或C ++声明
- 132. Palindrome Partitioning II
- 再读经典重构、重构与模式、修改代码的艺术《三》
- Linux下date格式化输出时间设置
- 基于visual c++之windows核心编程代码分析(17)通过pipe进程间通信
- 如何用Eclipse快速开发SSH框架项目
- c++学习之路
- ViewPager的小知识点及源码解析
- 数据泵卸载
- Android基础第六天