题目链接

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]的更多相关文章

  1. leetcode_目录

    3Sum Closest 3Sum 4Sum Add Binary Add Two Numbers Anagrams Balanced Binary Tree Best Time to Buy and ...

  2. leetcode dfs Palindrome Partitioning

    Palindrome Partitioning Total Accepted: 21056 Total Submissions: 81036My Submissions Given a string  ...

  3. 2019牛客暑期多校训练营(第六场)Palindrome Mouse 回文树+dfs

    题目传送门 题意:给出一个字符串,将字符串中所有的回文子串全部放入一个集合里,去重后.问这个集合里有几对<a,b>,使得a是b的子串. 思路:一开始想偏了,以为只要求每个回文串的回文后缀的 ...

  4. 2019牛客暑期多校训练营(第六场)C Palindrome Mouse (回文树+DFS)

    题目传送门 题意 给一个字符串s,然后将s中所有本质不同回文子串放到一个集合S里面,问S中的两个元素\(a,b\)满足\(a\)是\(b\)的子串的个数. 分析 首先要会回文树(回文自动机,一种有限状 ...

  5. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  6. Leetcode: Palindrome Partitioning II

    参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...

  7. Leetcode 131. Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  8. [Leetcode] Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  9. LeetCode Palindrome Permutation II

    原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...

随机推荐

  1. Golang闭包的坑

    team leader 发现一个Golang程序的bug,是由不正确使用闭包引起.记载一下,以作备忘. 猜猜一下程序的结果: import ( "fmt" "time&q ...

  2. python实现建立udp通信

    实现代码如下: #udp协议通信import socket,timeclass UdpConnect: def get_udp(self,ip,port,message): #建立udp连接 myso ...

  3. linux shell中的正则表达式

    正则表达式的使用 正则表达式,又称规则表达式.(英语:Regular Expression [ˈreɡjulə] 规则的 [ iksˈpreʃən] 表达 ),在代码中常简写为regex.regexp ...

  4. [Python3] 016 字典:给我一块硬盘,我可以写尽天下!

    目录 0 字典的独白 1 字典的创建 2 字典的特性 3 字典的常见操作 (1) 数据的访问.更改与删除 (2) 成员检测 (3) 遍历 4 字典生成式 5 字典的内置方法 6 可供字典使用的其它方法 ...

  5. Node.js中package.json中库的版本号详解(^和~区别)

    当我们查看package.json中已安装的库的时候,会发现他们的版本号之前都会加一个符号,有的是插入符号(^),有的是波浪符号(~).那么他们到底有什么区别呢?先贴一个例子,对照例子来做解释: &q ...

  6. 推荐 33 个 IDEA 最牛配置,写代码太爽了!

    作者:琦彦 blog.csdn.net/fly910905/article/details/77868300 1.设置maven 1.在File->settings->搜索maven 2. ...

  7. 【洛谷p1158】导弹拦截

    这道题是个有想法的枚举qwq 导弹拦截[题目链接] 注意:此导弹拦截非彼导弹拦截p1020 导弹拦截 一道题是1999年的,然后我们现在要写的是经过11年韬光养晦之后的导弹拦截 SOLUTION: 要 ...

  8. python基础面试题:(1)

    1.以下用C语言开发的Python解释器是( ) A:python是Java语言开发的Python解析器,B:PyPy是使用Python语言开发的Python解析,C:IronPython是.net平 ...

  9. FCKEditor报java.lang.NullPointerException

    1.需要在 加value=“ ” <FCK:editor instanceName="replycontent" basePath="/fckeditor" ...

  10. OC学习--面向对象的个人理解

    1. 什么是面向对象? 以下一段话是我在百度上找的解释: 面向对象(Object Oriented,OO)是软件开发方法.面向对象的概念和应用已超越了程序设计和软件开发,扩展到如数据库系统.交互式界面 ...