题目:

分割回文串

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

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

样例

给出 s = "aab",返回

[

["aa","b"],

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

]

解题:

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

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

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

时间复杂度O(n2)

Java程序:

public class Solution {
/**
* @param s: A string
* @return: A list of lists of string
*/
public ArrayList<ArrayList<String>> partition(String s) {
// write your code here
ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
if(s==null)
return result;
ArrayList<String> path = new ArrayList<String>();
helper(s,path,0,result);
return result;
}
private boolean isPalindrome(String s){
int beg = 0;
int end = s.length() - 1;
while(beg<end){
if(s.charAt(beg)!=s.charAt(end))
return false;
beg++;
end--;
}
return true;
}
private void helper(String s,ArrayList<String> path,int pos,ArrayList<ArrayList<String>> result){
if(pos==s.length()){
result.add(new ArrayList<String>(path));
return;
}
for(int i=pos+1;i<=s.length();i++){
String prefix = s.substring(pos,i);
if(!isPalindrome(prefix))
continue;
path.add(prefix);
helper(s,path,i,result);
path.remove(path.size()-1);
}
}
}

总耗时: 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. 设置datagridview中button按钮的背景颜色

    问题:DataGridViewButtonColumn()在datagridview中创建按钮列,如何设置按钮的背景颜色(不是单元格的背景颜色). 回答:可以在dataGridView1_CellPa ...

  2. [大牛翻译系列]Hadoop(16)MapReduce 性能调优:优化数据序列化

    6.4.6 优化数据序列化 如何存储和传输数据对性能有很大的影响.在这部分将介绍数据序列化的最佳实践,从Hadoop中榨出最大的性能. 压缩压缩是Hadoop优化的重要部分.通过压缩可以减少作业输出数 ...

  3. jquery鼠标滑过提示title具体实现代码

    jquery鼠标滑过提示title的实现代码. 如下: <script type="text/javascript" src="http://ajax.google ...

  4. div+css遮罩层

    曾被问到这个问题,不知所措,后来在网上找到了.大神文章:http://www.cnblogs.com/aspx-net/archive/2011/03/11/1981071.html 我想实现的效果没 ...

  5. Windows7鼠标右键里没有新建文本文件的选项,解决办法

    1.“开始”->“运行”,输入"regedit",打开注册表编辑器 2.展开HKEY_CLASSES_ROOT,找到.txt 3.选中.txt,查看右侧窗格的“默认值”是不是 ...

  6. Python支持中文注释

    三处设置,使Python的Eclipse开发环境(使用PyDev)支持中文 - (a)Eclipse的Window菜单Editors设置: Eclipse工具条 -> Window -> ...

  7. Google Code Jam 2015 Round1A 题解

    快一年没有做题了, 今天跟了一下 GCJ Round 1A的题目, 感觉难度偏简单了, 很快搞定了第一题, 第二题二分稍微考了一下, 还剩下一个多小时, 没仔细想第三题, 以为 前两个题目差不多可以晋 ...

  8. When to use Class.isInstance() & when to use instanceof operator?

    I think the official documentation gives you the answer to this one (albeit in a fairly nonspecific ...

  9. Scrum10-22

    Time:2013-10-22 Author:居玉皓 Things we have done since yesterday's meeting: 在今天的Scrum中,STORY1 开发前期准备工作 ...

  10. Oracle 中 for update 和 for update nowait 的区别

    原文出处http://bijian1013.iteye.com/blog/1895412 一.for update 和 for update nowait 的区别 首先一点,如果只是select 的话 ...