题目:

分割回文串

给定一个字符串s,将s分割成一些子串,使每个子串都是回文串。

返回s所有可能的回文串分割方案。

样例

给出 s = "aab",返回

[

["aa","b"],

["a","a","b"]

]

解题:

这个题目不好搞啊,需要动态规划

在这里,没有根据动态规划,也解决了,貌似是暴力解决

从下标pos开始,找到下标i使得 pos到i内是回文字符串,再从i+1开始,找到下一个回文串,这样一直找下去。。。

时间复杂度O(n2)

Java程序:

  1. public class Solution {
  2. /**
  3. * @param s: A string
  4. * @return: A list of lists of string
  5. */
  6. public ArrayList<ArrayList<String>> partition(String s) {
  7. // write your code here
  8. ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
  9. if(s==null)
  10. return result;
  11. ArrayList<String> path = new ArrayList<String>();
  12. helper(s,path,0,result);
  13. return result;
  14. }
  15. private boolean isPalindrome(String s){
  16. int beg = 0;
  17. int end = s.length() - 1;
  18. while(beg<end){
  19. if(s.charAt(beg)!=s.charAt(end))
  20. return false;
  21. beg++;
  22. end--;
  23. }
  24. return true;
  25. }
  26. private void helper(String s,ArrayList<String> path,int pos,ArrayList<ArrayList<String>> result){
  27. if(pos==s.length()){
  28. result.add(new ArrayList<String>(path));
  29. return;
  30. }
  31. for(int i=pos+1;i<=s.length();i++){
  32. String prefix = s.substring(pos,i);
  33. if(!isPalindrome(prefix))
  34. continue;
  35. path.add(prefix);
  36. helper(s,path,i,result);
  37. path.remove(path.size()-1);
  38. }
  39. }
  40. }

总耗时: 2017 ms

上面程序利用到的是深度优先遍历

DFS

(1) 判断是否结束

(2) for 循环取所有的子串

2.1 判断是否是回文字符串

2.2 是加入,递归进行

2.3 不是,for循环下一位 
 

lintcode:Palindrome Partitioning 分割回文串的更多相关文章

  1. 131 Palindrome Partitioning 分割回文串

    给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串.返回 s 所有可能的分割方案.例如,给出 s = "aab",返回[  ["aa"," ...

  2. Leetcode131. Palindrome Partitioning分割回文串

    给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa",&quo ...

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

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

  4. 分割回文串 · Palindrome Partitioning

    [抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 给出 s = "aab",返回 [ ["aa", & ...

  5. Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)

    Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning) 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. ...

  6. LeetCode 131. 分割回文串(Palindrome Partitioning)

    131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...

  7. Leetcode 132.分割回文串II

    分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一次分割就可将 s ...

  8. Leetcode 131.分割回文串

    分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa" ...

  9. 【LEETCODE】72、分割回文串 III 第1278题

    package y2019.Algorithm.dynamicprogramming.hard; /** * @Auther: xiaof * @Date: 2019/12/11 08:59 * @D ...

随机推荐

  1. CentOS6.4安装LAMP环境

    1.配置防火墙,开放80.3306端口 vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport - ...

  2. 主机win10与虚拟机ubuntu14.04通信

    主机是笔记本win10系统,在virtualbox虚拟机里面安装了ubuntu14.04系统,现在想让它们互联互通. 我的笔记本是通过路由器无线连接接入的互联网,设置了固定ip:192.168.0.4 ...

  3. struct和class区别

    转载来源:http://blog.sina.com.cn/s/blog_48f587a80100k630.html C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据 ...

  4. 5种IO模型

    Unix下可用的5种I/O模型分别是: 阻塞IO 非阻塞IO IO复用(select和poll) 信号驱动式IO(SIGIO) 异步IO(POSIX的aio系列函数)   阻塞式I/O模型:      ...

  5. 使用 PHP 验证表单数据

    使用 PHP 验证表单数据 首先我们对用户所有提交的数据都通过 PHP 的 htmlspecialchars() 函数处理. 当我们使用 htmlspecialchars() 函数时,在用户尝试提交以 ...

  6. MAC上 nodejs express 安装

    最近在MAC上搭建 nodejs环境以及安装 express 框架,遇到了一些问题,不过最后总算还是安装成功了,下面是操作步骤 1.node js 安装 访问nodejs官网进入下载mac上的安装包 ...

  7. Windows下关于Composer使用时出现的问题及解决办法

    问题一: Fatal error: Call to undefined method Composer\Package\CompletePackage::getTrans portOptions() ...

  8. ASP.NET Core 1.0

    .NET Core dotnet 命令大全:http://www.cnblogs.com/linezero/p/dotnet.html http://www.cnblogs.com/Wddpct/p/ ...

  9. Oracle 10g RAC 启动与关闭

    一. 检查共享设备 一般情况下,存放OCR和Voting Disk的OCFS2 或者raw 都是自动启动的. 如果他们没有启动,RAC 肯定是启动不了. 1.1 如果使用ocfs2的 检查ocfs2 ...

  10. openerp模块收藏 基于Lodop的报表打印模块(转载)

    基于Lodop的报表打印模块 原文:http://shine-it.net/index.php/topic,7397.0.html 前段时间写了个小模块,来解决OE中报表打印不方便的问题.借鉴了 @b ...