LeetCode Palindrome Partitioning II
2013-09-30 19:13
411 查看
Palindrome Partitioning IIMar
113297
/ 47208
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
the palindrome partitioning
be produced using 1 cut.
113297
/ 47208
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
1since
the palindrome partitioning
["aa","b"]could
be produced using 1 cut.
class Solution { private: void findpalindrome(const string &s, bool **flag, int cur, int len, vector< vector<string> >& res, bool *sign) { if(cur >= len - 1) { vector<string> tmp; for(int i = 0, prei = 0; i < len; i++) { if(sign[i] == true) { tmp.push_back(s.substr(prei, i - prei + 1)); prei = i + 1; } } res.push_back(tmp); } for(int i = cur + 1; i < len; ++i) { if(flag[cur+1][i]) { sign[i] = true; findpalindrome(s, flag, i, len, res, sign); sign[i] = false; } } } public: vector<vector<string>> partition(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<string> > res; if(s == "") return res; int len = s.size(); bool **flag = new bool*[len]; for(int i = 0; i < len; ++i) { flag[i] = new bool[len]; fill(flag[i], flag[i] + len, false); } //fill(&flag[0][0], &flag[len][len], false); /* for(int i = 0; i < len; ++i) { for(int j = i; j < len; ++j) { if(s[j] == s[i] && (j >= i || true == flag[i+1][j-1])) flag[i][j] = true; } }*/ for(int step = 0; step < len; ++step) { for(int i = 0; i < len - step; ++i) { int j = i + step; if(s[i] == s[j] && (i == j || i + 1 == j || true == flag[i+1][j-1])) flag[i][j] = true; } } bool *sign = new bool[len]; fill(sign, sign + len, 0); findpalindrome(s, flag, -1, len, res, sign); return res; } };
相关文章推荐
- [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 132 Palindrome Partitioning II
- Leetcode 132. Palindrome Partitioning II (Hard) (cpp)
- leetcode之 Palindrome Partitioning I&II
- Leetcode: Palindrome Partitioning I & II
- leetcode — palindrome-partitioning-ii
- LeetCode Palindrome Partitioning I&&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 132] - 回文分割II(Palindrome Partitioning II)
- 132. Palindrome Partitioning II Leetcode Python