Leetcode: Palindrome Partitioning II
来源:互联网 发布:家庭网络设计方案 编辑:程序博客网 时间:2024/06/05 20:50
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s = "aab"
,
Return 1
since the palindrome partitioning ["aa","b"]
could be produced using 1 cut.
直观思想,在I的基础上改进,不过怎么都过不了,超时,即使用了DP求回文,加上剪枝。
class Solution {public: int minCut(string s) { if (s.empty()) return 0; int cuts = -1; int size = s.size(); int mincuts = size - 1; vector<vector<bool>> dp(size, vector<bool>(size, false)); for (int i = size - 1; i >= 0; --i) { for (int j = i; j < size; ++j) { if (s[i] == s[j] && (j - i < 2 || dp[i+1][j-1])) { dp[i][j] = true; } } } partitionUtil(dp, s, 0, size - 1, cuts, mincuts); return mincuts; } void partitionUtil(vector<vector<bool>> &dp, string &s, int start, int end, int cuts, int &mincuts) { if (start > end) { if (cuts < mincuts) { mincuts = cuts; } } else { for (int i = start; i <= end; ++i) { if (dp[start][i] && cuts < mincuts) { partitionUtil(dp, s, i+1, end, cuts + 1, mincuts); } } } }};网上搜索,原来呀求最小cut仍然可以用DP,综合起来代码很简洁。DFS耗费时间太多了。
class Solution {public: int minCut(string s) { if (s.empty()) return 0; int size = s.size(); vector<int> cuts(size+1, 0); vector<vector<bool>> dp(size, vector<bool>(size, false)); for (int i = size - 1; i >= 0; --i) { cuts[i] = size - i; for (int j = i; j < size; ++j) { if (s[i] == s[j] && (j - i < 2 || dp[i+1][j-1])) { dp[i][j] = true; cuts[i] = min(cuts[i], 1 + cuts[j+1]); } } } return (cuts[0] - 1); }};给力的两篇分析,学习了。
http://blog.csdn.net/yutianzuijin/article/details/16850031
http://blog.csdn.net/u011095253/article/details/9177451
=====================第二次====================
class Solution {public: int minCut(string s) { int size = s.size(); vector<int> cuts(size+1, 0); vector<vector<bool>> palindromes(size, vector<bool>(size, false)); for (int i = size - 1; i >= 0; --i) { cuts[i] = size - i; for (int j = i; j < size; ++j) { if (s[i] == s[j] && (j - i < 2 || palindromes[i+1][j-1])) { palindromes[i][j] = true; cuts[i] = min(cuts[i], cuts[j+1] + 1); } } } return cuts[0] > 0 ? cuts[0] - 1 : 0; }};
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
- 怎样制作BT种子文件,即torrent后缀的文件?
- myeclipse中java文件头注释格式设置
- Exception in thread "main" java.lang.NullPointerException
- 多快好省用快播
- 体验Mac:PC上的苹果系统安装
- Leetcode: Palindrome Partitioning II
- 我的vimrc.vim备份
- linux命令行
- Training a deep autoencoder or a classifier on MNIST digits_Rbm训练(python)
- maven依赖包的查询
- StagefrightPlayer的创建
- java 虚拟机内存模型
- KMP模板
- 关于12306的一些想法