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 ...
随机推荐
- error U1087: cannot have : and :: dependents for same target
windows驱动开发遇到错误: 1>e:\winddk\7600\bin\makefile.new(7117) : error U1087: cannot have : and :: depe ...
- POJ 1838 Banana (并查集)
Description Consider a tropical forrest, represented as a matrix. The cell from the right top corner ...
- cocos2dx 3.1从零学习(六)——CocosStudio(VS2013project导入及环境设置)
导入libCocosStudio.libExtensions.libGUI 新建的project例如以下图: 加入现有项目 右键解决方式.例如以下操作: watermark/2/text/aHR0cD ...
- Vs2012于Linux应用程序开发(4):公共财产的定义
在嵌入式开发流程.有些参数基本上不改变,比如编译主机IP,username,password等参数.我们用VS提供的属性管理器来保存这些參数. 打开属性管理器: watermark/2/text/aH ...
- asp.net用户身份验证时读不到用户信息的问题 您的登录尝试不成功。请重试。 Login控件
原文:asp.net用户身份验证时读不到用户信息的问题 您的登录尝试不成功.请重试. Login控件 现象1.asp.net使用自定义sql server身份验证数据库,在A机器新增用户A,可以登录成 ...
- Hibernate 配置详解(11)
hibernate.session_factory_name_is_jndi 配置hibernate.cfg.xml中SessionFactory的name属性是否作为JNDI名称绑定.默认是true ...
- OpenCV两张图片的合并
转载请注明出处..! http://blog.csdn.net/zhonghuan1992 OpenCV两张图片的合并 原理: 两张图片合并,想想图片是用一个个像素点来存储.每一个像素点有他的值. 那 ...
- 运行一个android程序,直接访问某个网站
package android.com.youtube; import android.app.Activity; import android.content.Intent; import andr ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级) 企业库提供了一个很强大的验证应用程序模 ...
- Windows Phone开发(35):使用Express Blend绘图
原文:Windows Phone开发(35):使用Express Blend绘图 上一节中我们简单扯了一下绘图指令,然而那也不是最简单的绘图法,今天,我再向大家推荐一种更好的绘图方案--Express ...