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"]
]

解题:

使用深入优先搜索的思想;使用递归;

从头i = 0开始遍历字符串,找到满足要求的回文列,将其放入结果列表中,继续查找下面的回文字符;

解题步骤:

1、主函数,建立返回的二维数组,建立存放结果的一维数组,调用递归函数;

2、递归函数,输入为二维数组ret,一维数组path,字符串str,当前检查到的index值:

  (1)如果index值已经等于str的长度,说明搜索完毕,将path插入ret中,返回;

  (2)否则从i从index开始,遍历到str末尾,找index~i范围哪些是回文串:

    a. 将回文串摘出放入path中,更新index,继续递归;

    b. 从path中pop出放入的回文串,继续遍历循环;

3、返回ret

代码:

 class Solution {
public:
vector<vector<string>> partition(string s) {
vector<vector<string> > ret;
if(s.empty())
return ret; vector<string> path;
dfs(ret, path, s, ); return ret;
} void dfs(vector<vector<string> >& ret, vector<string>& path, string& s, int index) {
if(index == s.size()) {
ret.push_back(path);
return;
} for(int i = index; i < s.size(); ++i) {
// find all palindromes begin with index
if(isPalindrome(s, index, i)) {
path.push_back(s.substr(index, i - index + ));
dfs(ret, path, s, i+);
path.pop_back();
}
}
} bool isPalindrome(const string& s, int start, int end) {
while(start <= end) {
if(s[start++] != s[end--])
return false;
}
return true;
}
};
												

【Leetcode】【Medium】Palindrome Partitioning的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

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

  4. 【leetcode刷题笔记】Palindrome Partitioning II

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

  5. 【leetcode刷题笔记】Palindrome Partitioning

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

  6. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  7. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  8. 【LeetCode每天一题】Palindrome Number( 回文数字)

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

  9. 【leetcode刷题笔记】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  10. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

随机推荐

  1. VS2013使用EF6连接MySql

    前提:a.安装MySql的VS插件(版本请下载最新版) 我用的是:mysql-for-visualstudio-1.1.4 b.安装用于.net连接程序  mysql-connector-net-6. ...

  2. VerbalExpressions ——另类正则表达式

    对于文本处理来说,正则表达式无疑是一个非常强大的工具.但是编写和阅读正则表达式往往就不是那么一件非常愉快的事情了.本文在这里介绍另一种另类的正则表达式——VerbalExpressions,它采用函数 ...

  3. .gitignore配置

    .gitignore文件可以配置不希望加入git的文件,例如idea的.idea 工程文件 1.配置语法 以斜杠“/”开头表示目录: 以星号“*”通配多个字符: 以问号“?”通配单个字符 以方括号“[ ...

  4. display:inline-block

    /* inline为行内元素不自动换行,不占用文档流,也就是说你在这个后面写一个元素这个元素会并排显示.block为块元素,单独占一行文档,并可以给这个块元素添加宽高背景颜色.而inline-bloc ...

  5. ASP.NET Web API系列教程目录

    ASP.NET Web API系列教程目录 Introduction:What's This New Web API?引子:新的Web API是什么? Chapter 1: Getting Start ...

  6. JStrom的zk数据

    /jstorm/masterlock 用于LeaderSelector的锁. /jstorm/master get /jstorm/master localhost.localdomain:7627 ...

  7. 8.Mybatis的延迟加载

    在Mybatis中的延迟加载只有resultMap可以实现,ResultMap 可以实现高级映射(association,collection可以实现一对1和一对多的映射),他们具有延迟加载的功能,r ...

  8. Couldn't resolve Mac Server "mymac"

    vs2015创建一个iphone app ,Couldn't resolve Mac Server “mymac” 伤.下班走人

  9. git init 和 git init --bare 的区别

    http://blog.csdn.net/ljchlx/article/details/21805231 概念  裸仓储 :不可以在上面做git操作    the operation must be ...

  10. a==null和a.equals("null")的区别

    equals 是值比较,==是比较内存 A==B,比较句柄,就是比较变量A,B的地址存放的东西,比如int A=0;String B="bbbb";那么变量A的地址方的就是0,B的 ...