LeetCode: Palindrome Partitioning [131]
【称号】
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab"
,
Return
[
["aa","b"],
["a","a","b"]
]
【题意】
给定一个字符串s, 要求对s进行划分,划分后每一个子串都是回文串。
要求返回全部的划分情况
【思路】
直观思路是使用逐层递归。先确定第一个点。然后确定第二个点,再确定第三个点,依次类推。这样的方式的时间复杂度很高。
本题使用DP:先计算出随意两个位置i,j之间的子串是否是回文串,用IsPalindrome[i][j]表示。
【代码】
class Solution {
public: void getPartition(vector<vector<string> >&result, vector<string>&splits, int start, string&s, vector<vector<bool> >&isPal){
//spits-已切分的结果,start-当前切分開始的位置
if(start==s.length()){
vector<string> newSplits = splits;
result.push_back(newSplits);
return;
} for(int end=start; end<s.length(); end++){
if(isPal[start][end]){
splits.push_back(s.substr(start, end-start+1));
getPartition(result, splits, end+1, s, isPal);
splits.pop_back();
}
}
} vector<vector<string>> partition(string s) {
vector<vector<string> >result;
int len=s.length();
if(len==0)return result; vector<vector<bool> > isPal(len, vector<bool>(len, false));
//初始化isPal[i][i]=true;
for(int i=0; i<len; i++)
isPal[i][i]=true;
//初始化相邻两位字符构成的子串
for(int i=0; i<len-1; i++)
if(s[i]==s[i+1])isPal[i][i+1]=true;
//推断其它i<j的位置
for(int i=len-3; i>=0; i--)
for(int j=i+2; j<len; j++)
isPal[i][j]=(s[i]==s[j]) && isPal[i+1][j-1]; //确定全部的组合
vector<string> splits;
getPartition(result, splits, 0, s, isPal);
return result;
}
};
版权声明:本文博主原创文章。博客,未经同意不得转载。
LeetCode: Palindrome Partitioning [131]的更多相关文章
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [leetcode]Palindrome Partitioning II @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s ...
- [leetcode]Palindrome Partitioning @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/ 题意: Given a string s, partition s suc ...
- LeetCode: Palindrome Partitioning 解题报告
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode: Palindrome Partitioning II
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
- [Leetcode] Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- @font-face(css3属性)实如今网页中嵌入随意字体
@font-face语法规则 @font-face { font-family: <YourWebFontName>; src: <source> [<format> ...
- hadoop学习;自己定义Input/OutputFormat;类引用mapreduce.mapper;三种模式
hadoop切割与读取输入文件的方式被定义在InputFormat接口的一个实现中.TextInputFormat是默认的实现,当你想要一次获取一行内容作为输入数据时又没有确定的键.从TextInpu ...
- PHP获取当前页面完整的URL
#测试网址: http://localhost/blog/testurl.php?id=5 //获取域名或主机地址 echo $_SERVER['HTTP_HOST']."<br> ...
- I深搜
<span style="color:#330099;">/* I - 深搜 基础 Time Limit:1000MS Memory Limit:10000KB 64b ...
- [Unity3D]Unity4全新的动画系统Mecanim
Unity4.X添加一个新的动画系统,以取代原有的3.X旧的动画系统,全新的动画系统Mecanim是官方推荐,它使我们能够写更少的代码实现连续动画. 效果图 Unity3.X中动画系统播放动画 使用播 ...
- 数据验证validator 与 DWZ
在进行系统经常使用的数据验证.数据验证可以编写自己的,它也可以用来作为现在.现在,记录这两个库的使用, validator <!DOCTYPE HTML PUBLIC "-//W3C/ ...
- Java实现字符全阵列阵列
import org.junit.Test; public class AllSort { public void permutation(char[] buf, int start, int end ...
- UVA11100- The Trip, 2007
option=com_onlinejudge&Itemid=8&category=512&page=show_problem&problem=2041"> ...
- 解决android3.0版本号以上应用接收不到开机广播问题
如今是2014-07-16 下午15:27. 好久没写过东西,突然间灵感喷发想写点东西(事实上是刚刚弄好了一个棘手的问题,自豪中..呵呵呵呵 我牛掰).废话不多说,进入正题. 不知道你们又没有碰到这问 ...
- iOS Crash获取闪回日志和上传server
首先我们整理常常会闪退的异常哪些:数组越界.空引用.引用没有定义方法.内存空间不足等等. 怎样获取crash闪退日志 -- 工具查看 先看第一个问题怎样查看,我搜索的方法有下面几个: 第一个方法:XC ...