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 ...
随机推荐
- CentOS6.4安装LAMP环境
1.配置防火墙,开放80.3306端口 vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport - ...
- 主机win10与虚拟机ubuntu14.04通信
主机是笔记本win10系统,在virtualbox虚拟机里面安装了ubuntu14.04系统,现在想让它们互联互通. 我的笔记本是通过路由器无线连接接入的互联网,设置了固定ip:192.168.0.4 ...
- struct和class区别
转载来源:http://blog.sina.com.cn/s/blog_48f587a80100k630.html C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据 ...
- 5种IO模型
Unix下可用的5种I/O模型分别是: 阻塞IO 非阻塞IO IO复用(select和poll) 信号驱动式IO(SIGIO) 异步IO(POSIX的aio系列函数) 阻塞式I/O模型: ...
- 使用 PHP 验证表单数据
使用 PHP 验证表单数据 首先我们对用户所有提交的数据都通过 PHP 的 htmlspecialchars() 函数处理. 当我们使用 htmlspecialchars() 函数时,在用户尝试提交以 ...
- MAC上 nodejs express 安装
最近在MAC上搭建 nodejs环境以及安装 express 框架,遇到了一些问题,不过最后总算还是安装成功了,下面是操作步骤 1.node js 安装 访问nodejs官网进入下载mac上的安装包 ...
- Windows下关于Composer使用时出现的问题及解决办法
问题一: Fatal error: Call to undefined method Composer\Package\CompletePackage::getTrans portOptions() ...
- ASP.NET Core 1.0
.NET Core dotnet 命令大全:http://www.cnblogs.com/linezero/p/dotnet.html http://www.cnblogs.com/Wddpct/p/ ...
- Oracle 10g RAC 启动与关闭
一. 检查共享设备 一般情况下,存放OCR和Voting Disk的OCFS2 或者raw 都是自动启动的. 如果他们没有启动,RAC 肯定是启动不了. 1.1 如果使用ocfs2的 检查ocfs2 ...
- openerp模块收藏 基于Lodop的报表打印模块(转载)
基于Lodop的报表打印模块 原文:http://shine-it.net/index.php/topic,7397.0.html 前段时间写了个小模块,来解决OE中报表打印不方便的问题.借鉴了 @b ...