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. HTML5的兴起与4G网络的出现,能否够终止移动端的持续下滑走向

    HTML5的兴起与4G网络的出现,能否够终止移动端的持续下滑走向. 每当大家谈起互联网的未来的时候,多半谈及的是云.大数据.SAAS.仿佛要将一切摒弃.而当谈起移动互联网的时候.却坚持觉得NATIVE ...

  2. iOS Document Interaction 编程指南

    本文转载至 http://www.2cto.com/kf/201306/219382.html iOS支持在你的app中用其他app预览和显示文档.iOS还支持文件关联,允许其他app通过你的程序打开 ...

  3. LeetCode Problem 35:Search Insert Position

    描述:Given a sorted array and a target value, return the index if the target is found. If not, return ...

  4. 企业实施DevOPS的七大挑战(转)

    从别人的演讲视频中摘抄,做笔记. 什么是DevOPS 如何衡量DevOPS 企业实施DevOPS的七大挑战 自动化测试投入不足 单元测试 API测试 界面测试 功能测试 高度集中的IT服务 标准化 脚 ...

  5. ECMAScript/JS 基础知识讲解

    闭包 下面这个方法能输入0 1 2 ... 9吗?显然是不可以的,输出结果是10个10. function test() { var arr = []; for(var i = 0; i < 1 ...

  6. zookeeper 命令行使用

    先用简易的客户端链接上 默认链接的就是本机上面的zkserver 我一上来help 下.没办法 ,用的少,先喊一声救命 ,看看有没有人救我! [zk: localhost:2181(CONNECTED ...

  7. junit5荟萃知识点(一):junit5的组成及安装

    1.什么是junit5? 和之前的junit版本不一样,junit5是由三个模块组成. JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage ...

  8. 2014-08-28——Android和IOS的简单嗅探,以及横竖屏的捕获思路

    一般通过navigator.userAgent来嗅探Android系统和IOS系统: if(/android/i.test(navigator.userAgent)){ //android } if( ...

  9. Selenium chrome配置不加载图片

    from selenium import webdriver chrome_options = webdriver.ChromeOptions() prefs = {"profile.man ...

  10. 汽车数据的可视化分析(R)

    数据下载:http://www.fueleconomy.gov/feg/epadata/vehicles.csv.zip 将数据导入R中, 1.首先将工作路径设定到本地保存了vehicles.csv的 ...