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 ...
随机推荐
- Windows下MySQLroot密码破解
Win下MySQL修改root密码的多种方法 ##win2003mysql的密码破解 方法1: 用SET PASSWORD命令 mysql -u root mysql> SET PA ...
- linux下文件的复制、移动与删除
linux下文件的复制.移动与删除命令为:cp,mv,rm 一.文件复制命令cp 命令格式:cp [-adfilprsu] 源文件(source) 目标文件(destination) ...
- spring的配置
web.xml的配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&q ...
- WPF中的瀑布流布局(TilePanel)控件
最近在用wpf做一个metro风格的程序,需要用到win8风格的布局容器,只能自己写一个了.效果如下 用法 : <local:TilePanel ...
- PAT IO-03 整数均值
/* *PAT IO-02 整数四则运算 *2015.7.30 *作者:flx413 */ #include<stdio.h> int main() { ], sum; float ave ...
- 你的数据根本不够大,别老扯什么Hadoop了
本文原名"Don't use Hadoop when your data isn't that big ",出自有着多年从业经验的数据科学家Chris Stucchio,纽约大学柯 ...
- nodejs fs 模块的用途
/*** New node filefs 操作*/var fs = require(“fs”); /*创建文件 var fileName = “anps_hsj”;fs.mkdir(fileName, ...
- ORACLE 变量定义
DECLARE v_productid productinfo.productid%TYPE; v_productname ); v_productprice ,); v_quantity ); v_ ...
- 在ASP.NET MVC应用程序中实现Server.Transfer()类似的功能
在ASP.NET MVC应用程序中,如果使用Server.Transfer()方法希望将请求转发到其它路径或者Http处理程序进行处理,都会引发“为xxx执行子请求时出错”的HttpException ...
- ORACLE EXPDP命令使用详细【转】
本文转自:http://blog.csdn.net/zftang/article/details/6387325 ORACLE EXPDP命令使用详细 相关参数以及导出示例: 1. DIRECTORY ...