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 解释: 进行一 ...
随机推荐
- QT开发小技巧-窗口处理(一)
this->setWindowFlags(Qt::WindowCloseButtonHint); // 仅保留关闭按钮 this->setAttribute(Qt::WA_DeleteOn ...
- PLSQL导出表结构和数据的三种方式
1.导出表结构和数据方式1.tools->export user objects是导出表结构 tools ->export user object 选择选项,导出.sql文件 说明:导出的 ...
- wireshark 分析过滤数据
1.过滤IP,如来源IP或者目标IP等于某个IP例子:ip.src eq 192.168.1.107 or ip.dst eq 192.168.1.107或者ip.addr eq 192.168.1. ...
- 12.SpringMVC核心技术-请求转发和重定向
默认情况下,跳转到指定的View,使用的是请求转发.也可以显示的进行指出 此时,需在setViewName() 指定的视图前添加 forword: , 且此时的视图不会再与视图解析器中的前缀和后缀进 ...
- 5.SpringMVC 配置式开发-处理器适配器
处理器适配器HandlerAdapter 1.SimpleControllerHandlerAdapter(默认) 所有实现了 Controller 接口的处理器 Bean,均是通过SimpleCon ...
- mORMot学习笔记3 数据集转Json
usesSynCommons, SynDB, SynOleDB; procedure TForm1.DataToJsonClick(Sender: TObject); var Conn: TOleDB ...
- PAT Advanced 1154 Vertex Coloring (25 分)
A proper vertex coloring is a labeling of the graph's vertices with colors such that no two vertices ...
- 7.caffe:create_lmdb.sh(数据预处理转换成lmdb格式)
个人实践代码如下: #!/usr/bin/env sh # Create the imagenet lmdb inputs # N.B. set the path to the imagenet tr ...
- opengles reference card
https://www.khronos.org/files/opengles31-quick-reference-card.pdf https://www.khronos.org/opengles/s ...
- linear 工作流
最近把 最后一张画ui的rt 从float换成srgb 并没有引起我预计会有的 alpha混合结果发生变化的事情 我想是因为 1.artists在ps里的工作流是线性空间 2.onchip memor ...