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 解释: 进行一 ...
随机推荐
- JavaScript获取数组索引
JavaScript获取数组索引: <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- django 权限控制精简版
视图代码: 视图代码 def index(request): return render(request,'index.html') def login(request): if request.me ...
- es 启动报错 内存太小
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] elastics ...
- mount -t proc none /proc
linux initrd里的init脚本中的第一句为: mount -t proc /proc /proc 作用是把proc这个虚拟文件系统挂载到/proc目录.这说明initrd需要用到/proc, ...
- 关于pycharm database查看db.sqlites文件提示:Driver class 'org.sqlite.JDBC' not found
系统重新安装后,启动pycharm存在各种问题,其中一个问题就是在Pycharm中的database里面不能查看sqlite数据库了: 经过一番查找终于找到了问题: 首先问题 是提示这样一个报错: 解 ...
- 记一次引用maven插件报错解决方法
1.报错信息如图: plugin org.springframework.boot:spring-boot-maven-plugin not found 2.解决方案: maven的配置文件[sett ...
- 剖析ajax
学过javascript和接触过后端PHP语言必然要用到ajax,这是必学的一门学科,AJAX指的是Asynchronous JavaScript and XML,它使用XMLHttpRequest对 ...
- 牛客练习赛51 C 勾股定理 (数学,结论)
链接:https://ac.nowcoder.com/acm/contest/1083/C来源:牛客网 题目描述 给出直角三角形其中一条边的长度n,你的任务是构造剩下的两条边,使这三条边能构成一个直角 ...
- js动画fireworks烟花
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- CentOS 6 自定义单实例 二进制方式 安装mariadb-5.5.59
系统平台: CentOS release 6.9 (Final) 内核 2.6.32-696.el6.x86_64 1.去官网下载适合的二进制包 http://mariadb.org/ mariadb ...