您的位置:首页 > 其它

leetcode -- Palindrome Partitioning II

2014-10-06 23:13 288 查看

指责别人,看清自己

[问题描述]

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.

[解题思路]

ans[i]表示s[i]至最后需要的划分数

注意:line6 "<="

int Solution::minCut(string s){
const int len = s.length();
bool pali[len][len];
int ans[len];
fill_n(&pali[0][0], len*len, false);
for (int i = 0; i <= len; i++)
ans[i] = len - i - 1;
for (int i = len - 1; i >= 0; i --){
for (int j = i; j < len; j ++){
pali[i][j] = s[i]==s[j]&&(j-i<2||pali[i+1][j-1]);
pali[i][j] == true?(ans[i] = min(ans[i], ans[j + 1] + 1)):(true);
}
}
return ans[0];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: