[Leetcode 131, Medium] Palindrome Partitioning
来源:互联网 发布:mac chm阅读器 最好的 编辑:程序博客网 时间:2024/06/07 11:55
Problem:
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"] ]
Analysis:
Solution:
C++:
bool IsPalindrome(const string& str) { for(int begin = 0, end = str.size() - 1; begin < end; ++begin, --end) if(str[begin] != str[end]) return false; return true; } void PopulatePalindromes(string str, vector<string>& partition, vector<vector<string> >& candidates) { for(int i = 1; i <= str.size(); ++i) { string prefix = str.substr(0, i); if(IsPalindrome(prefix)) { partition.push_back(prefix); if(i == str.size()) candidates.push_back(partition); else PopulatePalindromes(str.substr(i), partition, candidates); partition.erase(partition.end() - 1, partition.end()); } } } vector<vector<string> > partition(string s) { vector<vector<string> > candidates; vector<string> partition; PopulatePalindromes(s, partition, candidates); return candidates; }Java:
Python:
0 0
- [Leetcode 131, Medium] Palindrome Partitioning
- Leetcode OJ 131 Palindrome Partitioning [Medium]
- Leetcode 131. Palindrome Partitioning (Medium) (cpp)
- LeetCode: Palindrome Partitioning [131]
- 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
- Leetcode #131 Palindrome Partitioning
- 131 Palindrome Partitioning [Leetcode]
- LeetCode 131 Palindrome Partitioning
- Leetcode 131:Palindrome Partitioning
- 【LeetCode-131】Palindrome Partitioning
- Leetcode 131 Palindrome Partitioning
- Extensible Messaging and Presence Protocol (XMPP) 的实现
- iOS客户端与Web前端开发的异同
- 三维变形技术FFD算法详解及实现
- poj2967
- iOS- 详解文本属性Attributes
- [Leetcode 131, Medium] Palindrome Partitioning
- Android快速开发框架-AIO
- NOJ 1429 全排列的输出
- stucts中package和action的配置
- 黑马程序员——IO流--转换流和键盘输入、File类
- Javascript 阻止冒泡
- 考题一:研究对全排列着色的性质 问题 A: 首先需要生成 n 的全排列然后对 n 的全排列进行着色, 使得相邻的两个数只需用最少颜色就可以把相邻的两个数用那区分开. (这里相邻包含两层含义:同时在自然
- C 算法精介----链表->单链表
- Hibernate和IBatis对比