lintcode:Palindrome Partitioning 分割回文串
题目:
给定一个字符串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 是加入,递归进行
lintcode:Palindrome Partitioning 分割回文串的更多相关文章
- 131 Palindrome Partitioning 分割回文串
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串.返回 s 所有可能的分割方案.例如,给出 s = "aab",返回[ ["aa"," ...
- Leetcode131. Palindrome Partitioning分割回文串
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa",&quo ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 分割回文串 · Palindrome Partitioning
[抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 给出 s = "aab",返回 [ ["aa", & ...
- Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)
Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning) 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. ...
- LeetCode 131. 分割回文串(Palindrome Partitioning)
131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...
- Leetcode 132.分割回文串II
分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一次分割就可将 s ...
- Leetcode 131.分割回文串
分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa" ...
- 【LEETCODE】72、分割回文串 III 第1278题
package y2019.Algorithm.dynamicprogramming.hard; /** * @Auther: xiaof * @Date: 2019/12/11 08:59 * @D ...
随机推荐
- HTML5应用开发:JavaScript库iScroll教程
目录 1. iScroll介绍 2. 安装和使用 3. 简单的iScroll例子 4. Pinch & Zoom 5. Snap to element 6. iScroll 详细参数 1. i ...
- jQuery实现用户注册的表单验证
用户注册的表单往往是需要进行验证的,否则会有一些不否合规则的数据入库,后果会不堪设想,本文通过jquery来实现. <html> <head> <meta chars ...
- 【linux】学习笔记
2014.06.07 开机无法上网,每次都得 $ sudo ifconfig eth1 up $ sudo dhclient eth1 后面发现原来是网卡没设置开机启动 编辑/etc/sysconfi ...
- SQL JOB
数据库同步是一种比较常用的功能.以下结合我自己的体会整理的,如果有理解不完全或者有误的地方望大牛不理赐教.下面介绍的就是数据库同步的两种方式: 1.SQL JOB的方式 sql Job的方式同步数据库 ...
- show engine innodb status\G
mysql> show engine innodb status\G *************************** 1. row *************************** ...
- 从零开始学ios开发(十七):Storyboards(上)
在开始这章之前,先做个说明,从这篇开始,我所使用的xcode更新成了最新的版本,版本是4.6.1(4H512),如下: 大家可以打开自己电脑上的App Store,然后搜索xcode,第一个出现的就是 ...
- UIViewController没有随着设备一起旋转的原因
对于iPhone app,UIViewController类提供了基本的视图管理模式.当设备改变方向的时候view controller的视图会自动随之旋转的.如果视图和子视图的autoresizin ...
- iOS10相册相机闪退bug-b
iOS10系统下调用系统相册.相机功能,遇到闪退的情况,描述如下: This app has crashed because it attempted to access privacy-sensit ...
- linux 命令小结
chkconfig --list 查询所有服务运行情况 修改文件夹权限: 在Linux中,权限的所有者分为用户权限,组权限和其他权限,分别是用字母u, g, o 代表权限分为:读 r , 写 w , ...
- eclipse 下找不到或无法加载主类的解决办法[转]
转自:http://blog.sina.com.cn/s/blog_7ebc46500101gtff.html 有时候 Eclipse 会发神经,好端端的 project 就这么编译不了了,连 Hel ...