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的更多相关文章

  1. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  2. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  3. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  4. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  5. [leetcode]Palindrome Partitioning II @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s  ...

  6. [leetcode]Palindrome Partitioning @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/ 题意: Given a string s, partition s suc ...

  7. LeetCode: Palindrome Partitioning 解题报告

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  8. Leetcode: Palindrome Partitioning II

    参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...

  9. [Leetcode] Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  10. LeetCode: Palindrome Partitioning [131]

    [称号] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

随机推荐

  1. mongo-connector来同步mongo

    个人博客:https://blog.sharedata.info/ 最近需要做mongo之间的同步,因此还是选择之前的工具mongo-connectorgitHub文档:https://github. ...

  2. Python数据驱动ddt

    import ddtimport unittest """ddt模块包含了一个类的装饰器ddt和两个方法的装饰器: data:包含多个你想要传给测试用例的参数: file ...

  3. 【BZOJ2803】[Poi2012]Prefixuffix 结论题

    [BZOJ2803][Poi2012]Prefixuffix Description 对于两个串S1.S2,如果能够将S1的一个后缀移动到开头后变成S2,就称S1和S2循环相同.例如串ababba和串 ...

  4. 《从零开始学Swift》学习笔记(Day 29)——访问级别

    Swift 2.0学习笔记(Day 29)——访问级别 原创文章,欢迎转载.转载请注明:关东升的博客 访问级别: Swift提供了3种不同访问级别,对应的访问修饰符为:public.internal和 ...

  5. 安装mysql报错—解决方法:error while loading shared libraries: libssl.so.6

    for 32bit ln -sf /usr/lib/libssl.so.10 /usr/lib/libssl.so.6ln -sf /usr/lib/libcrypto.so.10 /usr/lib/ ...

  6. 单片机教程4.C语言基础以及流水灯的实现

    单片机教程4.C语言基础以及流水灯的实现 C语言,没接触过计算机编程语言的人会把它看的很神秘,感觉非常的难,而在我看来,C语言的逻辑和运算,就是小学水平,所以大家不要怕它,我尽可能的从小学数学逻辑方式 ...

  7. jQuery DOM 元素方法(get)

    jQuery DOM 元素方法 1..get() 获得由选择器指定的 DOM 元素. $(selector).get(index)index 可选.规定获取哪个匹配元素(通过 index 编号). 实 ...

  8. MySQL中redo日志

    重做日志用来实现事务的持久性,即ACID中的D,由两部分组成: 一是内存中的重做日志缓冲(redo log buffer)  易丢失 二是重做日志文件(redo log file) 持久的 InnoD ...

  9. Linux python3安装/shell脚本/if/循环/函数

    python3安装 安装过程 安装包: wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgztar -xvf Python-3.7 ...

  10. python 中 for使用小技巧

    testDict = {i: i * i for i in xrange(10)} testSet = {i * 2 for i in xrange(10)} print(testSet) print ...