Leetcode: Palindrome Partitioning
来源:互联网 发布:在哪里编程游戏 编辑:程序博客网 时间:2024/05/29 14:33
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab"
,
Return
[ ["aa","b"], ["a","a","b"] ]
随手写了一个,没考虑时间复杂度,觉得应该过不了,结果竟然过了。简单的DFS。
class Solution {public: vector<vector<string>> partition(string s) { vector<vector<string>> result; if (s.empty()) return result; vector<string> partition; partitionUtil(result, partition, s, 0, s.size() - 1); } void partitionUtil(vector<vector<string>> &result, vector<string> &partition, string &s, int start, int end) { if (start > end) { result.push_back(partition); } else { for (int i = start; i <= end; ++i) { string str1 = s.substr(start, i - start + 1); if (isPalindrome(str1)) { partition.push_back(str1); partitionUtil(result, partition, s, i+1, end); partition.pop_back(); } } } } bool isPalindrome(const string &str) { for (int i = 0, j = str.size() - 1; i < j;) { if (str[i++] != str[j--]) { return false; } } return true; }};
=================第二次===============
相比之下,这种方法速度慢一些,感觉是因为vector的创建复制操作太多。
class Solution {public: vector<vector<string>> partition(string s) { vector<vector<bool>> palindromes(s.size(), vector<bool>(s.size(), false)); for (int i = s.size() - 1; i >= 0; --i) { for (int j = i; j < s.size(); ++j) { if (s[i] == s[j] && (j - i < 2 || palindromes[i+1][j-1])) { palindromes[i][j] = true; } } } return partitionUtil(palindromes, s, 0, s.size() - 1); } vector<vector<string>> partitionUtil(vector<vector<bool>> &palindromes,const string &s, int start, int end) { vector<vector<string>> partitions; for (int i = start; i <= end; ++i) { if (palindromes[start][i]) { string left = s.substr(start, i - start + 1); vector<vector<string>> rights = partitionUtil(palindromes, s, i+1, end); if (i >= end) { rights.push_back(vector<string>()); } for (int j = 0; j < rights.size(); ++j) { vector<string> comb; comb.push_back(left); comb.insert(comb.end(), rights[j].begin(), rights[j].end()); partitions.push_back(comb); } } } return partitions; }};第一种方法:
class Solution {public: vector<vector<string>> partition(string s) { vector<vector<string>> partitions; if (s.empty()) { return partitions; } vector<vector<bool>> palindromes(s.size(), vector<bool>(s.size(), false)); for (int i = s.size() - 1; i >= 0; --i) { for (int j = i; j < s.size(); ++j) { if (s[i] == s[j] && (j - i < 2 || palindromes[i+1][j-1])) { palindromes[i][j] = true; } } } vector<string> comb; partitionUtil(partitions, comb, palindromes, s, 0, s.size() - 1); return partitions; } void partitionUtil(vector<vector<string>> &partitions, vector<string> &comb, vector<vector<bool>> &palindromes,const string &s, int start, int end) { if (start > end) { partitions.push_back(comb); return; } for (int i = start; i <= end; ++i) { if (palindromes[start][i]) { comb.push_back(s.substr(start, i - start + 1)); partitionUtil(partitions, comb, palindromes, s, i+1, end); comb.pop_back(); } } }};
0 0
- 【leetcode】Palindrome Partitioning && Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning && Palindrome Partitioning II
- LeetCode Palindrome Partitioning I&&Palindrome Partitioning II
- LeetCode Palindrome Partitioning, Palindrome Partitioning II
- [Leetcode][python]Palindrome Partitioning/Palindrome Partitioning II
- Leetcode:Palindrome Partitioning & Palindrome Partitioning II
- LeetCode: Palindrome Partitioning
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode 122: Palindrome Partitioning
- leetcode:Palindrome Partitioning II
- leetcode:Palindrome Partitioning
- 【leetcode】Palindrome Partitioning
- 【leetcode】Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- Leetcode - Palindrome Partitioning
- LeetCode之Palindrome Partitioning
- [LeetCode]Palindrome Partitioning
- 诺基亚确实可以当榔头用,验证了
- PCB导入CAD文件 定义PCB板型
- 【官网最新翻译】使用fragment来创建一个灵活的用户界面
- jquery $.fn $.fx是什么意思
- js分页
- Leetcode: Palindrome Partitioning
- [HTML5]canvas元素中beginPath()方法
- jquery.fn.extend与jquery.extend
- 从一个字符串中获取中文
- 【Codility】Max-slice-sum★
- 一切成功源于积累——20140110 美加实盘(1)
- ListView 编程: 如何优化自定义 Adapter
- java set 删除元素 java.util.ConcurrentModificationException
- java 通过用jdbc:odbc方式连接MSSQL2008 Connection refused: connect 问题处理