您的位置:首页 > 其它

LEETCODE: Palindrome Partitioning II

2015-01-06 16:29 246 查看
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.
参考了别人的代码,关于回文的问题,一直不是很清楚。需要深入研究一下!

class Solution {
public:
int minCut(string s) {
if(s.length()==0||s.length()==1) {
return 0;
}
vector<vector<int>> palindrome_map(s.length(), vector<int>(s.length()));
vector<int> cut_num_array(s.length() + 1);

for(int ii=s.length() - 1; ii >= 0;ii --) {
cut_num_array[ii] = s.length() - ii;
for(int jj = ii; jj < s.length(); jj ++) {
if(s[ii] == s[jj]) {
if(jj - ii < 2 || palindrome_map[ii + 1][jj - 1] == 1) {
palindrome_map[ii][jj] = 1;
cut_num_array[ii] = min(cut_num_array[ii], cut_num_array[jj + 1] + 1);
}
}
}

}

return cut_num_array[0] - 1;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LEETCODE 算法 回文