LeetCode 131. 分割回文串(Palindrome Partitioning)
题目描述
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。
返回 s 所有可能的分割方案。
示例:
输入: "aab"
输出:
[
["aa","b"],
["a","a","b"]
]
解题思路
回溯思想。首先遍历字符串的各个子字符串,记录它们是否为回文串,然后对字符串各个索引递归判断回文串并加入到结果集合中。
代码
class Solution {
public:
vector<vector<string>> partition(string s) {
vector<vector<string>> res;
vector<vector<int>> strHuiwen(s.length(), vector<int>(s.length(), ));
vector<string> temp;
for(int i = ; i < s.length(); i++)
for(int j = i; j < s.length(); j++)
if(isHuiwen(s.substr(i, j - i + )))
strHuiwen[i][j] = ;
huiwen(s, , res, temp, strHuiwen);
return res;
}
void huiwen(string s, int idx, vector<vector<string>> &res, vector<string> &temp, vector<vector<int>> strHuiwen){
if(idx == s.length()){
res.push_back(temp);
return;
}
for(int i = idx; i < s.length(); i++){
if(strHuiwen[idx][i]){
temp.push_back(s.substr(idx, i - idx + ));
huiwen(s, i + , res, temp, strHuiwen);
temp.pop_back();
}
}
}
bool isHuiwen(string s){
for(int i = ; i < s.length() / ; i++)
if(s[i] != s[s.length() - i - ]) return false;
return true;
}
};
LeetCode 131. 分割回文串(Palindrome Partitioning)的更多相关文章
- LeetCode 131. 分割回文串(Palindrome Partitioning)
131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...
- Java实现 LeetCode 131 分割回文串
131. 分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa ...
- 分割回文串 · Palindrome Partitioning
[抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 给出 s = "aab",返回 [ ["aa", & ...
- Leetcode 131.分割回文串
分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa" ...
- [Swift]LeetCode131. 分割回文串 | Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)
Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning) 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. ...
- [LeetCode] 132. 分割回文串 II
题目链接 : https://leetcode-cn.com/problems/palindrome-partitioning-ii/ 题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子 ...
- Leetcode 132.分割回文串II
分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一次分割就可将 s ...
- Java实现 LeetCode 132 分割回文串 II(二)
132. 分割回文串 II 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一 ...
随机推荐
- 深入理解hadoop之mapreduce
本文系原创,若有转载需要,请注明出处.https://www.cnblogs.com/bigdata-stone/ 1.mapReduce简介 MapReduce是面向大数据并行处理的计算模型.框架和 ...
- linux Linux入门
Linux入门 Linux学习什么? 常用命令(背会) 软件安装(熟练) 服务端的架构(开开眼界) Linux如何学习? 不要问那么多为什么,后面你就懒得问了 先尝试理解一下,不行就背下来 一个知识点 ...
- PropertiesUtils(普遍做法)
public class PropertiesUtil{ private static Properties properties; static{ InputStream in = null; tr ...
- oracle索引查询
/*<br>* *查看目标表中已添加的索引 * */ --在数据库中查找表名 select * from user_tables where table_name like 'table ...
- 如何在调用Marketing Cloud contact创建API时增加对扩展字段的支持
需求:扩展字段"微信ID"是我创建出来的extension field,我想用Marketing Cloud提供的contact creation API,在创建contact时也 ...
- 【坑】maven编码配置
错误环境: maven 3.5.0 idea 2017.1.1 错误原因: 由于没有设置统一编码,导致与其他同事开发过程中出现乱码问题 解决方案: 在maven的 pom配置中properties节点 ...
- Eclipse创建Maven项目时,项目中只存在src/main/resources(没有src/main/java、src/test/java)的解决方法
例:Maven项目(chapter11),发现只存在src/main/resources,缺少了src/main/java和src/test/java 解决方法: 1.eclipse->wi ...
- (备忘)Eclipse设置:背景与字体大小和xml文件中字体大小调整
Eclipse中代码编辑背景颜色修改: 1.打开window / Preference,弹出Preference面板 2.展开General标签,选中Editors选项,展开. 3.选中 Text ...
- sklearn--回归
一.线性回归 LinearRegression类就是我们平时所说的普通线性回归,它的损失函数如下所示: 对于这个损失函数,一般有梯度下降法和最小二乘法两种极小化损失函数的优化方法,而scikit-le ...
- python爬取豆瓣电影信息数据
题外话+ 大家好啊,最近自己在做一个属于自己的博客网站(准备辞职回家养老了,明年再战)在家里 琐事也很多, 加上自己 一回到家就懒了(主要是家里冷啊! 广东十几度,老家几度,躲在被窝瑟瑟发抖,) 由于 ...