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 =
Return
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;
}
};
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s =
"aab",
Return
1since 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] Palindrome Partitioning II
- leetcode Palindrome Partitioning II
- Palindrome Partitioning II Leetcode
- leetcode Palindrome Partitioning II
- [LeetCode]132 Palindrome Partitioning II
- LeetCode之Palindrome Partitioning II
- Leetcode: Palindrome Partitioning II
- [LeetCode]Palindrome Partitioning II
- LeetCode132 Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning && Palindrome Partitioning II
- [Leetcode]Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning II
- LeetCode Palindrome Partitioning II
- [leetcode刷题系列]Palindrome Partitioning II
- Leetcode237: Palindrome Partitioning II
- Leetcode 132 Palindrome Partitioning II
- Leetcode 132. Palindrome Partitioning II (Hard) (cpp)
- [Leetcode][python]Palindrome Partitioning/Palindrome Partitioning II
- leetcode:Palindrome Partitioning II
- leetcode Palindrome Partitioning II DP 深搜