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. 《从零开始学Swift》学习笔记(Day 9)——离开表达式你试试!

    原创文章,欢迎转载.转载请注明:关东升的博客 表达式啊是很重要地. 在Swift中,表达式有3种形式. 不指定数据类型 var a1 = 10 指定数据类型 var a1:Int  = 10 使用分号 ...

  2. JS复制粘贴解决方案

    var clipboardData = window.clipboardData; //for IE if (!clipboardData) { // for chrome window.prompt ...

  3. 关于Apache Shiro权限框架的一些使用误区的解释

    多了不说了,进入正题,shiro是个权限框架提供权限管理等功能,网上的教程一般都是互相抄,比如<shiro:principal property="xxx"/>这个标签 ...

  4. 删除datatable中的行

    今天遇到一问题,无论如何也删除不干净datatable.我想全部删除.但总是得不到想要的结果. ; i < dt.Rows.Count; i++) { //dt.Rows.Remove(dt.R ...

  5. Python3.6全栈开发实例[018]

    18.车牌区域划分, 现给出以下车牌.根据车牌的信息, 分析出各省的车牌持有量.(升级题) result = {} for car in cars: location = locals[car[0]] ...

  6. 常用模块一(os模块、序列化模块(json和pickle))

    一.os模块 os模块是与操作系统交互的一个接口. import os # 和文件和文件夹的操作有关 os.makedirs('dirname1/dirname2') # 可生成多层递归目录 os.r ...

  7. Linux中权限管理之文件属性权限

    chattr [+-=][选项] 文件或目录名 + 增加权限 - 删除权限 = 等于某权限 选项: i 文件设置i属性,不允许对文件进行删除.改名.添加.修改数据,相当于把整个文件锁起来了 目录设置i ...

  8. 通过实例来分析I2C基本通信协议

    本文旨在用最通俗易懂的方式.让大家明确I2C通信的过程到底是怎么回事. I2C起源于飞利浦公司的电视设计,但之后朝通用路线发展,各种电子设计都有机会用到I2C 总的来说,I2C能够简单归纳为,两根线, ...

  9. Use Private Members from Base Class

    最近看了一段代码:在导出类中调用继承自基类的某个public函数,该函数中对基类中的private数据成员 进行了赋值并将结果打印.看到程序的运行结果后,顿时感觉自己之前(我之前的理解这里就不说明了) ...

  10. Python之正则表达式与常用模块(Day19)

    一.正则表达式:匹配字符串的一种规则 正则表达式的在线测试工具: http://tool.chinaz.com/regex/ 字符组: 正则 待匹配字符 匹配结果 说明 [0123456789] 8 ...