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 ...
随机推荐
- Swagger入门教程(转)
[译]5.41 Swagger tutorial 单击此处查看原文 更多概念参见:Implementing Swagger with your API docs 关于 Swagger Swagger能 ...
- HttpServletRequest中的request.setAttribute()和request.getSession().setAttribute()
request.setAttribute("num",value):有效范围是一个请求范围,不发送请求的界面无法获取到value的值,jsp界面获取使用EL表达式${num}:re ...
- TP框架对数据库的基本操作
数据库的操作,无疑就是连接数据库,然后对数据库中的表进行各种查询,然后就是对数据的增删改的操作,一步步的讲述一下框架对数据库的操作 想要操作数据库,第一步必然是要:链接数据库 一.链接数据库 (1)找 ...
- JDK11 | 第四篇 : 增强API
文章首发于公众号<程序员果果> 地址 : https://mp.weixin.qq.com/s/O0xntC-JfeSBk-9x2QfwnA 一.简介 JDK 9~11 在语言语法方面有一 ...
- utf-8 bom头问题 thinkphp 报错 Namespace declaration statement has to be the very first statement in the script
先看百度百科上的解释---- BOM —— Byte Order Mark,中文名译作“字节顺序标记”.在这里找到一段关于 BOM 的说明: 在UCS 编码中有一个叫做 "Zero Widt ...
- SpringMVC Controller单例和多例(转)
首先上测试代码 import org.springframework.context.annotation.Scope; import org.springframework.stereotype.C ...
- Spring boot集成Swagger,并配置多个扫描路径
Spring boot集成Swagger,并配置多个扫描路径 1:认识Swagger Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目 ...
- OtterTune源码解析
为了方便后面对ottertune进行魔(hu)改(gao),需要先搞清楚它的源码结构和pipeline OtterTune分为两大部分: server side: 包括一个MySQL数据库(用于存储调 ...
- vue打开新窗口
1. <router-link tag="a" target="_blank" :to="{name:'detail',query:{goods ...
- vue主动刷新页面及列表数据删除后的刷新方法
在继刷新当前页面,重载页面数据那篇之后 那一篇 深入理解数据驱动 以上算是开发过程中的一个坑,用了一段时间,今天再读代码的时候,感觉被坑的很严重. 1. 获取列表方法 2.重新获取数据 3.这样再次调 ...