LeetCode---Palindrome Partitioning
来源:互联网 发布:中国移动学院网络大学 编辑:程序博客网 时间:2024/05/21 10:08
题目大意:给出一个字符串,对该字符串进行各种切割,使得切割后的每个子串是回文,找出所有的情况。
算法思想:
整体上采用深度优先搜索的思想。遍历字符串的每一个元素,判断从该元素处断开是否是一个合法方案(即构成回文)若是则递归搜索剩下一部分字符串的可行方案。
代码如下:
class Solution {public: vector<vector<string> > partition(string s) { vector<vector<string> >res; vector<string> path; dfs(s,res,path,0); return res; } void dfs(string s,vector<vector<string> >&res,vector<string> &path,size_t start ){ if(start==s.size()){ res.push_back(path); return ;}for(int i=start;i<s.size();++i){if(isPalindrome(s,start,i)){path.push_back(s.substr(start,i-start+1));dfs(s,res,path,i+1); path.pop_back();}} }bool isPalindrome(string s,size_t start,size_t end){while(start<end&&s[start]==s[end]){++start;--end;} return start<end?false: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
- StarUML使用说明-指导手册
- pat甲 1078 Hashing
- 困扰我多次的内存溢出问题终于解决了
- 断点续传
- 在WPF中获取程序的专用工作集内存 PerformanceCounter
- LeetCode---Palindrome Partitioning
- 模糊测试相关文章
- AXI4-Stream to Video Out 调试
- linux 查看文件夹大小
- Windows中的system函数
- C语言函数入栈顺序
- Internet控制协议(计算机网络学习笔记)
- SSH无密码登录
- 五年计划