方法一: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. 如何让VS2012同时调试2个项目

  2. python 统计单词个数

    根据一篇英文文章统计其中单词出现最多的10个单词. # -*- coding: utf-8 -*-import urllib2import refrom collections import Coun ...

  3. 肾果手机App Store切换区域(无需Visa或者万事达)

    8月份在肾果官网买了个touch6,有时候需要换区去墙外下载app,然而一个个国家都要输入Visa或者万事达卡...今天终于找到一个不用输入信用卡号的区域:Canada!!! 办法(适用于8.X,7. ...

  4. distinct 去重复查询——两个表join 连接,去掉重复的数据

    ------distinct 去重复查询 select * from  accounts acc join (select distinct accid from roles) r on r.acci ...

  5. IndexReader和IndexWriter的生命周期

    http://youyang-java.iteye.com/blog/1731205 对于IndexReader而言,反复使用 IndexReader .open打开会有很大的开销,所以一般在整个程序 ...

  6. PHP file_exists() 函数

    定义和用法 file_exists() 函数检查文件或目录是否存在. 如果指定的文件或目录存在则返回 true,否则返回 false. 语法 file_exists(path) 参数 描述 path ...

  7. ps 命令使用总结

    ps命令用来查看进程信息,它是类似于快照类型的只显示一次,如果想及时刷新请用top命令. 1. 常用参数列表 -a 显示所有终端机下执行的进程,除了阶段作业领导者之外. a 显示现行终端机下的所有进程 ...

  8. IIS UrlWriter配置(asp.net)

    前提在建虚拟目录或网站时注意以下设置第一步:下载URLRewriter 添加URLRewriter和ActionlessForm(不添加只能在VS实现,IIS下会找不到页面). 第二步:配置web.c ...

  9. MySQL 授权详解

    (1)确认一下3306是否对外开放,mysql默认状态下是不开放对外访问功能的.查看的办法如下: 1 2 3 4 5 6 7 netstat -an | grep 3306 tcp        0  ...

  10. MySQL information_schema表查询导致内存暴涨

    case:下面的一条sql语句,导致mysql实例内存暴涨: select * from tables where table_name not in(select table_name from p ...