leetcode: Palindrome Partitioning II
来源:互联网 发布:看小说软件 编辑:程序博客网 时间:2024/06/01 08:11
这题在Palindrome Partitioning的基础上还需要一次动态规划。
dp[i][j]保存i到j是否是回文
dp2[i]保存从0到i需要的最小cut数
状态转换方程:dp[i] = min(dp[j])+1 ( 0=< j < i)
class Solution {public: int minCut(string s) { if( s.size() <= 1) return 0; int size = s.size(); vector< vector< bool> > dp( size, vector< bool>( size, false)); for( int i = 0; i < size; ++i) dp[i][i] = true; for( int i = 0; i < size; ++i){ for( int j = 0; j < i; ++j){ if( s[j] == s[i] && dp[j+1][i-1]){ dp[j][i] = true; } if( s[j] == s[i] && j == i - 1){ dp[j][i] = true; } } } vector< int> dp2(size, 0); int tmp = 0; for( int i = 0; i < size; ++i){ if(dp[0][i]){ dp2[i] = 0; continue; } tmp = 0x7fffffff; for( int j = 0; j < i; ++j){ if(dp[j+1][i]){ if(tmp > dp2[j]+1){ tmp = dp2[j] + 1; } } dp2[i] = tmp; } } return dp2[size-1]; }};
0 0
- 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 II
- [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 II
- LeetCode Palindrome Partitioning II
- [leetcode]Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning II
- 百度的搜索原理
- 让你提前认识软件开发(23):如何在C语言中执行shell命令?
- 【redis源码分析】数字集合--intset
- uva 10400 Game Show Math (填合适的运算符)
- Longest Ordered Subsequence (最长有序(递增)序列)
- leetcode: Palindrome Partitioning II
- poj2031(最小生成树)
- MongoDB_09之计数去重分组等
- CreateDirectory(…)执行失败,GetLastError()返回错误码5
- UVa 216 - Getting in Line
- -jQuery图片九宫格样式鼠标悬停图片滑动切换效果
- java泛型总结
- Oracle JOB定时任务
- WebGL开源项目研究之ViziCities