方法一:DFS递归,判断每一个是否为回文数
1,首先要有一个判断字符串是否是回文的函数。容易实现,字符串从两边同时往中间走,看字符是否相同;
2,深度优先搜索思想对字符串进行遍历。得到结果。例如,s = "abacd"; 需要对“a”“ad”“aba”“abac”“abacd”进行深度优先搜索。深度搜索的过程如下:先选“a”,发现“a”是回文,则深度遍历“bacd”,继续深度遍历,“b”也是回文,深度遍历“acd”,继续下去;
同时,深度遍历“a”的同时,也需要深度遍历“ab”,“aba”,“abac”,“abacd”。发现只有“aba”是回文,所以继续深度搜索“cd”。 1 class Solution {
public:
vector<vector<string>> result;
void DFS(string str, vector<string>& curSubStrs, int start, int end)
{
if(start>end)
{
result.push_back(curSubStrs);
return;
}
for(int k=start;k<=end;k++)
{
string r=str.substr(start,k-start+);
if(palindrom(r))
{
curSubStrs.push_back(r);
DFS(str,curSubStrs,k+,end);
curSubStrs.pop_back();
}
}
}
bool palindrom(string r)
{
int n=r.size()-;
int s=;
while(s<n)
{
if(r[s]!=r[n])
return false;
s++;
n--;
}
return true;
}
vector<vector<string>> partition(string s) {
int n=s.size();
result.clear();
if(n<) return result;
vector<string> tmp;
DFS(s,tmp,,n-);
return result;
}
};
方法二:也是递归,只不过是换个方式的方法
 //方法二,同样递归,但是只是处理小技巧上方法,其实第一次是想这么做得
class Solution {
public:
vector<vector<string>> res;
vector<vector<string>> partition(string s) {
int n=s.size();
vector<string> tmp;
dfs(s,tmp);
return res;
}
bool palindrom(string r)
{
int n=r.size()-;
int s=;
while(s<n)
{
if(r[s]!=r[n])
return false;
s++;
n--;
}
return true;
}
void dfs(string s,vector<string>&tmp)
{
if(s.size()<) //s是空串
{
res.push_back(tmp);
return;
}
for(int i=;i<s.size();i++)
{
string st=s.substr(,i+); //i+1是长度,从0开始,长度是i+1,其实主要是递归的思想,小的递归和大的递归实际是一样的,所以做题时可以根据最大的递归来想最小的递归是否正确
if(palindrom(st))
{
tmp.push_back(st);
dfs(s.substr(i+),tmp); //没有第一个参数时就是从i+1到组后
tmp.pop_back();
} }
}
}; 方法三:采用动态规划的方法
举例:aabc,这里动态规划是dict[i][j]表示从i到j是否为回文,如果i=0,j=3,就先判断s[i]是否等于s[j],如果j-i<=2,则说明i,j是连着,或一个数,或3个数,如a,ab,aab这种,
只用判断s[i]是否等于s[j]即可,如果j-i>2则判断dict[i+1][j-1]是否为回文,所以以2维数组代表i到j,因为需要先知道后面的,所以要从后向前,又j从题意上是要大于等于i,
所以有:
for(int i=n-1;i>=0;i--)
{
fro(int j=i;j<n;j++)
{
............
}
}
注意substr(start,i-start+1),错了好几次,是从start开始,长度是i-start+1,用substr(start,i+1),是不行的,因为后面start会增加,只有第一个start=0是正确,后面就不对
了如aaabc,start=2时,本来想要a,如果i+1,就成了3,从start开始长度为3就是abc了,所以应该是i-start+1为长度,是1正确.
代码如下:
 //方法三,提前采用动态规划,这样可以不用后来每个都递归判断,能减少重复判断
class Solution {
public:
vector<vector<string>> res;
vector<vector<string>> partition(string s) {
int n=s.size();
if(n<=) return res; vector<vector<bool>> dict(n,vector<bool>(n,false));
for(int i=n-;i>=;i--)
{
for(int j=i;j<n;j++)
{
if(s[i]==s[j]&&(j-i<=||dict[i+][j-]))
{
dict[i][j]=true;
}
}
}
vector<string> tmp;
dfs(s,dict,,tmp);
return res;
}
void dfs(string &s,vector<vector<bool>>& dict,int start,vector<string>& tmp)
{
if(start==s.length())
{
res.push_back(tmp);
return;
}
for(int i=start;i<s.length();i++)
{
if(dict[start][i])
{
tmp.push_back(s.substr(start,i-start+));
dfs(s,dict,i+,tmp);
tmp.pop_back();
}
}
} };

leetcode之Palindrome Partitioning的更多相关文章

  1. [LeetCode] 131. Palindrome Partitioning 回文分割

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  2. [Leetcode Week13]Palindrome Partitioning

    Palindrome Partitioning 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/palindrome-partitioning/desc ...

  3. Leetcode 131. Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  4. 【leetcode】Palindrome Partitioning II(hard) ☆

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  5. [Leetcode][JAVA] Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  6. 【leetcode】Palindrome Partitioning II

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  7. 【leetcode】Palindrome Partitioning

    Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...

  8. leetcode 132. Palindrome Partitioning II ----- java

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  9. leetcode之 Palindrome Partitioning I&II

    1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...

  10. [LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

随机推荐

  1. JS-------DOM0级事件处理和DOM2级事件处理-------简单记法

    0级DOM 分为2个:一是在标签内写onclick事件  二是在JS写onlicke=function(){}函数 1) <input id="myButton" type= ...

  2. hdu 2594 Simpsons’ Hidden Talents KMP应用

    Simpsons’ Hidden Talents Problem Description Write a program that, when given strings s1 and s2, fin ...

  3. Unity3d Shader开发(三)Pass(Pass Tags,Name,BindChannels )

    Pass Tags 通过使用tags来告诉渲染引擎在什么时候该如何渲染他们所期望的效果. Syntax 语法 Tags { "TagName1" = "Value1&qu ...

  4. C#热血传奇游戏服务端再次开源更新

    2014年新春佳节即将到来,也算是送给大家的一份新年礼物.虽然这礼物貌似不给力啊哈哈.(没有用心啊 o(∩_∩)o 哈哈) 这次开源主要去掉上一次开源版本中大量指针代码,简化上手操作,并重构大部分代码 ...

  5. SQL 中With as 的用法

    转自:http://www.cnblogs.com/superyinhai/archive/2010/04/09/1708643.html 一.WITH AS的含义 WITHAS短语,也叫做子查询部分 ...

  6. 服务器部署_centos 安装nginx手记

    前言: a.linux上安装nginx网上有很多文章,本文仅仅是自己整理备忘. b.安装centos的时候,把develop相关组件都装上,免得缺这个缺哪个. c. 本文软件版本:nginx-1.2. ...

  7. [状压dp]POJ1185 炮兵阵地

    中文题 题意不再赘述 对于中间这个“P” 根据dp的无后效性 我们只需考虑前面的 就变成了 只需考虑: 也就是状压前两行 具体与HDOJ的4539类似: 看HDOJ 4539 仅仅是共存状态的判断不同 ...

  8. hdu 4878 ZCC loves words AC自动机+中国剩余定理+快速幂

    题意就不说了. 分析:折腾好几天自己写的代码还是看了别人代码后发现几乎没什么复杂度的差别,可是就是一直超时,后来干脆照着别人写啊,一直WA,就在准备放弃干脆先写这篇博客的时候,又看了一眼WA的代码,发 ...

  9. [codility]tree_height

    http://codility.com/demo/take-sample-test/treeheight 非常非常简单的求树的深度.不忍直视. // you can also use includes ...

  10. RAM云存储已经出现了,就是特别贵

    据说bat有这种全内存的服务器集群, RAM的问题是,容量上去了就会很费电,而且不方便做持久化,并且成本也不低,一般只能作为缓存.内存数据库,给bat.12306这种高富帅用 也有提供内存云存储的服务 ...