题目:

分割回文串

给定一个字符串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. NSS_09 gridpanel中的actioncolumn事件

    在设计角色权限时, 终于用到了grid的actioncolumn,如下: { header: '权限设定', xtype: 'actioncolumn', items: [{ icon: 'Conte ...

  2. Winform ListView根据条件定位到指定行

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. PHP中数组排序实例学习

    先介绍下php中用于数组排序的函数: 排序方法                           升序                             降序                 ...

  4. android 线程

    android线程: 通用多个线程通信管理框架: 1.Handler监听者框架:子线程是事件源,主线程是监听者.        Handler作为子线程的监听器出现:主线程中生成Handler的子类, ...

  5. 每日一“酷”之heapq

    作用:heapq模块实现一个适用于Python列表的最小堆排序算法 堆(heap)是一个属性数据结构,其中子节点与父节点是一种有序关系.二叉堆是一种特殊的堆,二叉堆是完全二元树(二叉树)或者是近似完全 ...

  6. WPF-数据绑定:日期时间格式

    WPF-数据绑定:日期时间格式绑定后自定义格式的例子. 我刚才遇到的问题是绑定完之后,星期始终显示为英文.需要一个属性ConverterCulture制定区域. 如下: {Binding dateti ...

  7. WPF中的DataTemplate

    <Window x:Class="DateTemplate应用.MainWindow" xmlns="http://schemas.microsoft.com/wi ...

  8. QQ群里收集的外企iOS开发的笔试题

    一组外企iOS开发的笔试题,您能回答出来吗?从群里收集来的. 1 why can't NSArray contain NSInteger Instance? with which extra step ...

  9. javascript之流程控制 和函数的容易忽略点

    1.流程控制 1> for in  仅用于 对象的遍历: var box={ "name":'小红', 'age':18, 'height':165 }; for(var b ...

  10. mysql中实现oracle中的rowid功能

    mysql中没有函数实现,只能自己手动添加变量递增  := 就是赋值,只看红色字体就行 select @rownum:=@rownum+1,img.img_path,sku.sku_name from ...