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 ...
随机推荐
- Gradle 修改 Maven 仓库地址(转)
近来迁移了一些项目到Android Studio,采用Gradle构建确实比原来的Ant方便许多.但是编译时下载依赖的网速又着实令人蛋疼不已. 如果能切换到国内的Maven镜像仓库,如开源中国的Mav ...
- awk使用的实例
1.使用split函数 name.url的内容: 上海 http://trip.elong.com/shanghai/jingdian elong destination 云南 htt ...
- 屌丝程序猿赚钱之道之taobao 2
续上篇,之前写的案例,都是比較0基础的. 案例4: 代写情书.软文.论文等等. 这是我一个同学的真实故事. 我隔壁寝室的小王平时没事就爱谢谢博客.逛逛论坛.大二的时候接触了威客网,開始在网上 ...
- Golang初学者的资源整理
看了汪汪汪不是我的语言的GO语言零基础入门资料整理,个人感觉还不够全面,忍不住过来补充一些内容. 网站教程: GO语言编程 and GO语言开发2048 from 实验楼Go语言后台应用开发 form ...
- 编译安装LNMP Centos 6.5 x64 + Nginx1.6.0 + PHP5.5.13 + Mysql5.6.19
(来自:http://www.cnblogs.com/vicowong/archive/2011/12/01/2116212.html) 环境: 系统硬件:vmware vsphere (CPU:2* ...
- hdu3709(数位dp)
求区间[l,r]内有多少个数的满足: 选一个位为中点,是的左边的数到该位的距离等于右边的位到该位的距离. 比如4139 选择3位中点, 那么左边的距离是 4 * 2 + 1 * 1 , 右边的距 ...
- .net读取异步Post的内容
//读取微信Post过来的XML内容 byte[] input = HttpContext.Current.Request.BinaryRead(HttpContext ...
- 使用ToggleButton和StackPanel+Border实现圆角开关按钮动画效果
<ToggleButton Height=" HorizontalAlignment="Left" Margin="138,122,0,0" N ...
- 简述负载均衡&CDN技术(转)
曾经见到知乎上有人问“为什么像facebook这类的网站需要上千个工程师维护?”,下面的回答多种多样,但总结起来就是:一个高性能的web系统需要从无数个角度去考虑他,大到服务器的布局,小到软件中某个文 ...
- 经典排序算法 - 高速排序Quick sort
经典排序算法 - 高速排序Quick sort 原理,通过一趟扫描将要排序的数据切割成独立的两部分,当中一部分的全部数据都比另外一部分的全部数据都要小,然后再按此方法对这两部分数据分别进行高速排序,整 ...