LeetCode 131. Palindrome Partitioning
来源:互联网 发布:老版本mac如何升级系统 编辑:程序博客网 时间:2024/06/07 14:11
题目:
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
思路:主要是DFS搜索,假设到s[0~i]时是回形文,对 i 以后的的string分别进行判断能否组成回形文进行切割并存入临时变量里,一直到string尾为止,把结果加入最终的数组中,并退回进行搜索,退回时记得要删除临时变量里面已经记录过得string,相当于树的搜索,只是分支时是自己判断是否进行分支。
class Solution {public: bool ispalindrome(const string& s, int start, int end) { while(start<=end) { if(s[start++]!=s[end--]) return false; } return true; } void dfs(string &s,int index,vector<string> &path,vector<vector<string>> & re) { if(index==s.size()) { re.push_back(path); return; } for(int i=index;i<s.size();i++) { if(ispalindrome(s, index, i)) { path.push_back(s.substr(index, i - index + 1)); dfs(s, i+1, path, re); path.pop_back(); } } } vector<vector<string>> partition(string s) { vector<vector<string>> re; int len=s.size(); if(len==0) return re; vector<string> path; dfs(s, 0, path, re); return re; }};
阅读全文
0 0
- 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
- [LeetCode] 131. Palindrome Partitioning
- Leetcode-131. Palindrome Partitioning
- 【LeetCode】131. Palindrome Partitioning
- Leetcode 131. Palindrome Partitioning
- GIT——基本操作
- oracle 中 Flashback table功能恢复某个表数据!!
- hdoj1045 Fire Net(dfs)
- 解决PHP报错ZipArchive library is not enabled异常的方法
- 经典算法之插入排序(直接插入排序、shell排序)
- LeetCode 131. Palindrome Partitioning
- js删除数组中的元素
- [四]Spring Boot 整合Mybatis
- 运行mongodb创建data库时出现计算机丢失api-ms-win-crt-runtime-|1-1-0.dll
- MyEclipse自动生成访问器
- 2.搭建开发环境所需的工具
- Delphi程序引用C#:报缺DCU文件。。。
- hashcode和equals重写规则
- tomcat 部分知识点记录