LeetCode——palindrome-partitioning
Question
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s ="aab",
Return
[
["aa","b"],
["a","a","b"]
]
Solution
求解思想用的是DFS,然后需要记录已经判断过是回文的子字符串,具体实现需要慢慢的体会。
Code
class Solution {
public:
vector<vector<string>> partition(string s) {
vector<vector<string>> res;
vector<string> path;
dfs(0, s, path, res);
return res;
}
void dfs(int index, string s, vector<string> path, vector<vector<string>>& res) {
if (index == s.length()) {
res.push_back(path);
return;
}
for (int i = index; i < s.length(); i++) {
if (isPalindrome(s, index, i)) {
path.push_back(s.substr(index, i - index + 1));
dfs(i + 1, s, path, res);
path.pop_back();
}
}
}
bool isPalindrome(string s, int start, int end) {
while (start <= end) {
if (s[start++] != s[end--])
return false;
}
return true;
}
/*
bool isPalindrome(string s, int start, int end) {
if (start >= end)
return true;
if (s[start] != s[end])
return false;
return isPalindrome(s, start++ , end--);
}
*/
};
LeetCode——palindrome-partitioning的更多相关文章
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [leetcode]Palindrome Partitioning II @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s ...
- [leetcode]Palindrome Partitioning @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/ 题意: Given a string s, partition s suc ...
- LeetCode: Palindrome Partitioning 解题报告
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode: Palindrome Partitioning II
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
- [Leetcode] Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- LeetCode: Palindrome Partitioning [131]
[称号] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
随机推荐
- 【BZOJ2005】[Noi2010]能量采集 欧拉函数
[BZOJ2005][Noi2010]能量采集 Description 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把 ...
- 利用Google Analytics API实现自己的统计报表
Google Analytics 简称 GA,功能实在是太强大了,正因如此,导致调研GA API花费了大量的时间,太多的名词需要梳理. 正确的学习步骤是: 首先,找个有权限的账号,登录GA(https ...
- Grafana---graph
主面板简单的命名为Graph.它提供了一组非常丰富的图形选项. 单击面板的标题将显示一个菜单.edit选项为面板打开了额外的配置选项. 一.General general允许定制面板的外观和菜单选项. ...
- Java的==与equals之辨
=======================原理速记=================================== equals重载后比较内容,==比较指针.否则equls等同于== (Ja ...
- CentOS 7.4 下安装Epel源和Nginx
EPEL (Extra Packages for Enterprise Linux)是基于Fedora的一个项目,为“红帽系”的操作系统提供额外的软件包,适用于RHEL.CentOS和Scientif ...
- 启动hadoop集群
1.配置core-site.xml(每个节点上都要配置) 路径: /usr/local/hadoop-2.7.3/etc/hadoop/core-site.xml 配置项1: name: fs.def ...
- centos7安装nodejs 和 yarn
如何从EPEL库安装Node.js 另一个有效且简单的方法来安装Node.js就是从官方库.这同样确保您可以访问到EPEL库,你可以通过运行以下命令. sudo yum install epel-re ...
- k8s 常用命令
[root@master ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION master Ready master 1h v1.8.1 node1 ...
- app开发团队人员构成怎么分配?国内著名的app开发团队有哪些
app开发团队人员构成:作为一个独立的app开发团队,人员架构必须包括产品经理,程序开发人员,测试专员,运营团队,UI 设计.这里是对专业的App开发公司而言,一般个人或团队可能一个人会身兼多职,所以 ...
- pip3命令报错Fatal error in launcher: Unable to create process using '"d:\old_files\py3.6\python.exe" "E:\py3.6\Scripts\pip3.exe" list'
cmd输入pip3 list命令报错 Fatal error in launcher: Unable to create process using '"d:\old_files\py3.6 ...