Leetcode 131. Palindrome Partitioning (Medium) (cpp)
来源:互联网 发布:powerdvd源码输出 编辑:程序博客网 时间:2024/06/05 10:24
Leetcode 131. Palindrome Partitioning (Medium) (cpp)
Tag: Backtracking
Difficulty: Medium
/*131. Palindrome Partitioning (Medium)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"]]*/class Solution {public: vector<vector<string>> partition(string s) { int _size = s.size(); vector<vector<bool>> t(_size, vector<bool>(_size, false)); for (int j = 0; j < _size;j++) { for (int i = 0; i < _size;i++) { if (i + j < _size) { if (j == 0) { t[i][i + j] = true; } else if (j < 3) { t[i][i + j] = (s[i] == s[i + j]); } else { t[i][i + j] = (s[i] == s[i + j]) && (t[i + 1][i + j - 1]); } } } } vector<vector<string>> res; vector<string> res_sub; partition(res, res_sub, t, 0, s); return res; }private: void partition(vector<vector<string>>& res, vector<string>& res_sub, const vector<vector<bool>>& t, int index, const string& s) { if (index == t[0].size()) { res.push_back(res_sub); return; } for (int i = index; i < t[0].size(); i++) { if (t[index][i]) { res_sub.push_back(s.substr(index, i - index + 1)); partition(res, res_sub, t, i + 1, s); res_sub.pop_back(); } } }};
0 0
- Leetcode 131. Palindrome Partitioning (Medium) (cpp)
- 131. Palindrome Partitioning -Medium
- [Leetcode 131, Medium] Palindrome Partitioning
- Leetcode OJ 131 Palindrome Partitioning [Medium]
- LeetCode 131. Palindrome Partitioning
- [LeetCode]131.Palindrome Partitioning
- [Leetcode] 131. Palindrome Partitioning
- [leetcode] 131.Palindrome Partitioning
- [LeetCode]131. Palindrome Partitioning
- 131. Palindrome Partitioning LeetCode
- Leetcode 131. Palindrome Partitioning
- LeetCode 131. Palindrome Partitioning
- LeetCode *** 131. Palindrome Partitioning
- LeetCode 131. Palindrome Partitioning
- LeetCode-131.Palindrome Partitioning
- 【leetcode】131. Palindrome Partitioning
- [leetcode] 131. Palindrome Partitioning
- 【Leetcode】131. Palindrome Partitioning
- 13-NSPersistentContainer性能比较
- 14-CoreData兼容iOS9和iOS10
- BeautifulSoup的高级应用 之 find findAll
- 搬家了,以后 在 www.JiFei.co 上写文章了。。。
- LeetCode 465. Optimal Account Balancing
- Leetcode 131. Palindrome Partitioning (Medium) (cpp)
- Volley 解析
- STL之map实现(一)
- STL之map实现(二)
- Sqoop 各大命令解释
- 前段时间做项目中遇到问题的随笔分享
- 狼人入门简单正向思维分析第一夜怎么不鱼的度过
- STL之map实现(三)
- [leetcode 26]Remove Duplicates from Sorted Array——移除排序数组中重复元素