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

  1. [
  2. ["aa","b"],
  3. ["a","a","b"]
  4. ]

解题:

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

从头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

代码:

  1. class Solution {
  2. public:
  3. vector<vector<string>> partition(string s) {
  4. vector<vector<string> > ret;
  5. if(s.empty())
  6. return ret;
  7.  
  8. vector<string> path;
  9. dfs(ret, path, s, );
  10.  
  11. return ret;
  12. }
  13.  
  14. void dfs(vector<vector<string> >& ret, vector<string>& path, string& s, int index) {
  15. if(index == s.size()) {
  16. ret.push_back(path);
  17. return;
  18. }
  19.  
  20. for(int i = index; i < s.size(); ++i) {
  21. // find all palindromes begin with index
  22. if(isPalindrome(s, index, i)) {
  23. path.push_back(s.substr(index, i - index + ));
  24. dfs(ret, path, s, i+);
  25. path.pop_back();
  26. }
  27. }
  28. }
  29.  
  30. bool isPalindrome(const string& s, int start, int end) {
  31. while(start <= end) {
  32. if(s[start++] != s[end--])
  33. return false;
  34. }
  35. return true;
  36. }
  37. };
  1.  

【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. 如何防止ElasticSearch集群出现脑裂现象(转)

    原文:http://xingxiudong.com/2015/01/05/resolve-elasticsearch-split-brain/ 什么是“脑裂”现象? 由于某些节点的失效,部分节点的网络 ...

  2. 阿里巴巴分布式服务框架Dubbo介绍(1)主要特色

    引言 互联网服务和BS架构的传统企业软件相比,系统规模上产生了量级的差距.例如 传统BS企业内部门户只需要考虑数百人以及几千人的访问压力,而大型互联网服务有时需要考虑的是千万甚至上亿的用户: 传统企业 ...

  3. 8天掌握EF的Code First开发系列之2 Code First开发系列之领域建模和管理实体关系

    本文出自8天掌握EF的Code First开发系列,经过自己的实践整理出来. 本篇目录 理解Code First及其约定和配置 创建数据表结构 管理实体关系 三种继承模式 本章小结 本人的实验环境是V ...

  4. apache+tomcat分布式搭建

    windows 下Apache和tomcat整合 负载均衡session共享 准备工作: 1. Apache 2.2.4 下载地址:http://cztele1.skycn.com/down/apac ...

  5. UVa 10071 - Back to High School Physics

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=s ...

  6. [转]linux /proc/cpuinfo 文件分析

    在Linux系统中,提供了proc文件系统显示系统的软硬件信息.如果想了解系统中CPU的提供商和相关配置信息,则可以通过/proc/cpuinfo文件得到.本文章针对该文件进行简单的总结. 基于不同指 ...

  7. python语句表达式——黑板客老师课程学习

    1.赋值 多重赋值: a,b=1,2 a,b=’beijing’,’sh’ a,b=’bj’ a,b=(1,2) a,b=[1,2] …… 2.输入输出 输入: raw_input()   原始输入 ...

  8. Linux内核模块简介

    一. 摘要 这篇文章主要介绍了Linux内核模块的相关概念,以及简单的模块开发过程.主要从模块开发中的常用指令.内核模块程序的结构.模块使用计数以及模块的编译等角度对内核模块进行介绍.在Linux系统 ...

  9. C++(MFC)中WebBrowser去除3D边框的方法(实现IDocHostUIHandler接口)控制 WebBrowser 控件的外观和行为

    在 CSDN 上经常看到以下两个问题:1.在 MFC 应用程序中,如果创建了一个 WebBrowser 控件(包括 CHtmlView 在内),如何可以把该控件的三维边框禁止掉?2.在 MFC 应用程 ...

  10. Python—元组tuple

    列表的知识其实就类似于c语言中的数组,可插入.修改.list=[a,b,c,d] 而元组tuple,一旦初始化即不可修改.好处与绝对安全. 定义一个空的元组:t=() 定义只有一个元素的元组:t=(1 ...