[Leetcode]Palindrome Partitioning
来源:互联网 发布:php培训后容易找工作吗 编辑:程序博客网 时间:2024/05/22 17:03
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> > res; vector<vector<string>> partition(string s) { vector<string> partitions; dfs(partitions, s, 0); return res; } void dfs(vector<string> partitions, string &s, int idx) { if (idx >= s.size()) { if (partitions.size() > 0) { res.push_back(partitions); } return; } for (int i = idx; i < s.size(); ++i) { string cur = s.substr(idx, i - idx + 1); if (isPalindrome(cur)) { partitions.push_back(cur); dfs(partitions, s, i + 1); partitions.pop_back(); } } return; } bool isPalindrome(string s) { int len = s.size(); if (len <= 1) return true; int left = 0; int right = len - 1; while (left < right) { if (s[left] != s[right]) return false; ++left; --right; } return true; }};
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
- 乐村淘电商平台
- 有序数组或者有序数组的旋转数组 查找最小值
- 【POJ1700】Crossing River 贪心,附贪心问题的一系列详细解析
- 郝斌老师C语言零基础自学专讲180集完整版
- 完成端口服务
- [Leetcode]Palindrome Partitioning
- HIVE优化总结
- FNDLOAD 笔记
- 【P1738】NOIP2011Mayan游戏C++题解(搜索)
- JavaScript回顾(7)
- HDU 2159 FATE(二维dp背包)
- Android权限之二packages.xml解析
- 有关js的history,无刷新跳到页面顶部
- unity3d模型制作规范