lintcode-136-分割回文串
136-分割回文串
给定一个字符串s,将s分割成一些子串,使每个子串都是回文串。
返回s所有可能的回文串分割方案。样例
给出 s = "aab",返回
[
["aa", "b"],
["a", "a", "b"]
]标签
回溯法 深度优先搜索
思路
使用回溯和递归
code
class Solution {
public:
/**
* @param s: A string
* @return: A list of lists of string
*/
vector<vector<string>> partition(string s) {
// write your code here
int size = s.size();
if(size <= 0) {
return vector<vector<string> >();
}
vector<vector<string> > result;
vector<string> temp;
partition(s, 0, temp, result);
return result;
}
void partition(string s, int current, vector<string> &temp, vector<vector<string> > &result) {
if(current == s.size()){
result.push_back(temp);
return;
}
for(int i=current; i<s.size(); i++) {
if(isPalindrome(s, current, i)) {
temp.push_back(s.substr(current,i-current+1));
partition(s, i+1, temp, result);
temp.pop_back();
}
}
}
bool isPalindrome(string s, int begin, int end) {
for(int i=begin, j=end; i<j; i++, j--) {
if(s[i] != s[j]) {
return false;
}
}
return true;
}
};
lintcode-136-分割回文串的更多相关文章
- lintcode:Palindrome Partitioning 分割回文串
题目: 分割回文串 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa&q ...
- 分割回文串 · Palindrome Partitioning
[抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 给出 s = "aab",返回 [ ["aa", & ...
- Leetcode 132.分割回文串II
分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一次分割就可将 s ...
- Leetcode 131.分割回文串
分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa" ...
- Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)
Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning) 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. ...
- 【LEETCODE】72、分割回文串 III 第1278题
package y2019.Algorithm.dynamicprogramming.hard; /** * @Auther: xiaof * @Date: 2019/12/11 08:59 * @D ...
- LeetCode 131. 分割回文串(Palindrome Partitioning)
131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...
- [LeetCode] 132. 分割回文串 II
题目链接 : https://leetcode-cn.com/problems/palindrome-partitioning-ii/ 题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子 ...
- Java实现 LeetCode 132 分割回文串 II(二)
132. 分割回文串 II 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一 ...
- Java实现 LeetCode 131 分割回文串
131. 分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa ...
随机推荐
- Oracle 反向索引(反转建索引) 理解
一 反向索引 1.1 反向索引的定义 反向索引作为B-tree索引的一个分支,主要是在创建索引时,针对索引列的索引键值进行字节反转,进而实现分散存放到不同叶子节点块的目的. 1.2 反向索引针对的问题 ...
- Oracle 11g行字段拼接WMSYS.WM_CONCAT问题Not A LOB
Oracle 11g行字段拼接WMSYS.WM_CONCAT问题Not A LOB 一.问题出现 项目中的某个查询需要将表中某个字段不重复地拼接起来,百度得到该函数WMSYS.WM_CONCAT(字段 ...
- 使用poi读取excel文件 Cannot get a text value from a numeric cell
我这样转换得到一个excel文本域的值 Cell cell = row.getCell(c); cell.setCellType(Cell.CELL_TYPE_STRING); String park ...
- JDK9 新特性
JDK9 新特性目录导航 目录结构 模块化系统 jshell 多版本兼容JAR 接口的私有方法 改进try-with-resourcs 改进砖石操作符 限制使用单独下划线标识符 String存储结构变 ...
- is 和 isinstance的区别 and issubclass
定义一个子类和父类 class A: pass class B(A): pass is print(type(b) is B) # 结果: True print(type(b) is A) # 结果: ...
- 类的特殊方法"__new__"详解
上代码! class A: def __new__(cls, *args, **kwargs): obj = super().__new__(cls) print("__new__ &quo ...
- PHP (Yii2) 自定义业务异常类(可支持返回任意自己想要的类型数据)
public function beforeAction($action) { return parent::beforeAction($action); } public function runA ...
- Nodejs 使用 SerialPort 调用串口
工作经常使用串口读写数据,electron 想要替代原来的客户端,串口成了必须要突破的障碍. get --> https://github.com/EmergingTechnologyAdvi ...
- Hadoop(5)-Hive
在Hadoop的存储处理方面提供了两种不同的机制,一种是之前介绍过的Hbase,另外一种就是Hive,有关于Hbase,它是一种nosql数据库的一种,是一种数据库,基于分布式的列式存储,适合海量数据 ...
- c#vs连接SQL sever数据库入门操作
对于需要连接数据库的项目,可以参考的简单初级代码.实现打开数据库,读入数据功能 using System; using System.Collections.Generic; using System ...