Leetcode_131. Palindrome Partitioning_[DFS]
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
Example:
Input: "aab"
Output:
[
["aa","b"],
["a","a","b"]
] 题意简单明了。
解法:要找到所有的划分方法,应想到穷举。
class Solution {
public:
bool isPalindrome(string s){
for(size_t i=; i<s.size()/; i++)
if(s[i] != s[s.size()--i])
return false;
return true;
}
vector<vector<string>> partition(string s) {
vector<vector<string>> ret;
vector<string> temp;
dfs(s, , temp, ret);
return ret;
}
void dfs(string& s, int idx, vector<string>& temp, vector<vector<string>>& ret){
if(idx == s.size()){
ret.push_back(temp);
return;
}
for(size_t i=; i<=s.size()-idx; i++){
string prefix = s.substr(idx, i);
if(isPalindrome(prefix)){
temp.push_back(prefix);
dfs(s, idx+i, temp, ret);
temp.pop_back();
}
}
}
};
Leetcode_131. Palindrome Partitioning_[DFS]的更多相关文章
- leetcode_目录
3Sum Closest 3Sum 4Sum Add Binary Add Two Numbers Anagrams Balanced Binary Tree Best Time to Buy and ...
- leetcode dfs Palindrome Partitioning
Palindrome Partitioning Total Accepted: 21056 Total Submissions: 81036My Submissions Given a string ...
- 2019牛客暑期多校训练营(第六场)Palindrome Mouse 回文树+dfs
题目传送门 题意:给出一个字符串,将字符串中所有的回文子串全部放入一个集合里,去重后.问这个集合里有几对<a,b>,使得a是b的子串. 思路:一开始想偏了,以为只要求每个回文串的回文后缀的 ...
- 2019牛客暑期多校训练营(第六场)C Palindrome Mouse (回文树+DFS)
题目传送门 题意 给一个字符串s,然后将s中所有本质不同回文子串放到一个集合S里面,问S中的两个元素\(a,b\)满足\(a\)是\(b\)的子串的个数. 分析 首先要会回文树(回文自动机,一种有限状 ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode: Palindrome Partitioning II
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
- Leetcode 131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [Leetcode] Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- LeetCode Palindrome Permutation II
原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...
随机推荐
- (转) intellij idea部署web项目时的位置(Tomcat)
这篇文章说的比较好: 原文地址:https://blog.csdn.net/zmx729618/article/details/78340566 1.当你项目启动的时候console能看到项目运行的位 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_08 转换流_5_InputStreamReader介绍&代码实现
强转chart:类型 GBK
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第4节 ArrayList集合_18-练习三_按指定格式打印集合的方法
主要锻炼用集合作为参数 使用if判断语句
- 函数介绍——MulDiv
http://blog.sina.com.cn/s/blog_579ebc11010008ql.html 函数介绍——MulDiv (2007-03-27 10:05:30) 转载▼ 分类: 编程 ...
- 基于Quartz.net 的任务调度平台Weiz.TaskManager
Weiz.TaskManager https://github.com/weizhong1988/Weiz.TaskManager 任务管理平台 系统简介 Quartz.net是一个开源的任务调度工具 ...
- C++[Tarjan求点双连通分量,割点][HNOI2012]矿场搭建
最近在学图论相关的内容,阅读这篇博客的前提是你已经基本了解了Tarjan求点双. 由割点的定义(删去这个点就可使这个图不连通)我们可以知道,坍塌的挖煤点只有在割点上才会使这个图不连通,而除了割点的其他 ...
- mysql修改库、表、字段 字符集,中文排序
查看字段编码: show full columns from t2;show variables like '%character%';show variables like 'collation_% ...
- Maven-Eclipse使用maven创建HelloWorld Java项目,使用Junit-4.11的注解
1.针对前面创建的mavenTest项目,我们做一些修改,包括pom.xml.App.java.AppTest.java 说明:其中的scope属性,如果是test,表示该依赖只对测试有效,如果不声明 ...
- servlet到springmvc的演进
1.简单看看servlet 1.1.servlet继承关系 先看看下面servlet的这个继承关系,有点印象即可(可以暂时忽略ServletConfig,这个接口就是让我们可以从web.xml文件中拿 ...
- C Yuhao and a Parenthesis
Yuhao and a Parenthesis time limit per test 2 seconds memory limit per test 256 megabytes input stan ...