LeetCode------------palindrome-partitioningII(回文2)
来源:互联网 发布:unity3d vs2015 调试 编辑:程序博客网 时间:2024/06/06 01:16
题目描述
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",
Return1since the palindrome partitioning["aa","b"]could be produced using 1 cut.
class Solution {
public:
int minCut(string s) {
if (s.size()==0 || s.size()==1)
return 0;
vector<int> dp(s.size(), 0);
for (int i=1; i<s.size(); i++)//dp[i]初始化
dp[i] = isPalind(s.substr(0, i+1)) ? 0:i;
for (int i=1; i<s.size(); i++){
for (int j=0; j<=i; j++){
if (isPalind(s.substr(j+1, i-j)))
dp[i] = min(dp[i], dp[j] + 1);
else
dp[i] = min(dp[i], dp[j] + i-j);
}
}
return dp[s.size()-1];
}
int isPalind(string s){
if (s.size()==0 ||s.size()==1)
return 1;
int i=0;
int j=s.size()-1;
while(i<=j){
if (s[i] != s[j])
return 0;
else{
++i;
j--;
}
}
return 1;
}
};
- LeetCode------------palindrome-partitioningII(回文2)
- leetcode 回文系列 Palindrome
- 【回文】leetcode - Shortest Palindrome
- LeetCode------------palindrome-partitioning(回文)
- 【Leetcode】回文数字Palindrome
- [LeetCode] 回文数字 Palindrome Number
- leetcode:Palindrome Number 回文整数
- leetcode:Valid Palindrome 合法回文
- [LeetCode] Palindrome Partitioning 回文分割
- LeetCode - Valid Palindrome (判断“回文”)
- LeetCode Palindrome Number回文数
- LeetCode (16) Valid Palindrome (回文)
- [Leetcode] Palindrome Permutation 回文变换
- Leetcode-- Palindrome Number 回文数
- [Leetcode] Longest Palindrome 最长回文
- LeetCode 9 palindrome (回文数)
- leetcode Palindrome Number 回文数
- leetcode【Palindrome Partitioning 回文字符串划分】
- Python笔记--String、list、dict排序
- NOIP提高组【JZOJ4813】running
- 虚拟机类加载机制
- Spring IOC
- angular.js学习(1)--directive指令独立scope
- LeetCode------------palindrome-partitioningII(回文2)
- 二、基本数据类型、运算符和表达式
- 在python34下beautifulsoup的安装
- HDU 5510 Bazinga(KMP)
- 内存区域
- 查找一个单向链表中倒数第K个元素
- Android中的属性动画(Property Animation)
- JFinal Web开发学习(三)前后台路由设计
- 垃圾回收算法