方法一: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. nginx错误日志error_log日志级别

    error_log 级别分为 debug, info, notice, warn, error, crit  默认为crit,

  2. 使用phpize安装php模块

    1.下载包 2./usr/local/php/bin/phpize 3../configure --enable-soap  --with-php-config=/usr/local/php/bin/ ...

  3. Delphi窗体中禁用最大化按钮

    第一种方法是设置窗体的BorderIcons/biMaximize属性为False,这种方法仅让窗体的最大化按钮灰掉: 第二种方法是设置窗体的BorderStyle属性为bsDialog,这种方法使最 ...

  4. RSA使用 常识

    1公钥加密,私钥解密  OK反过来, 私钥加密,公钥解密 也OK 2 使用RSA加密 对称算法的key ,用对称算法加密 消息.伙伴收到消息后,RSA解密出 对称算法的key,再用这个key去解密消息 ...

  5. 支持阻塞操作和轮询操作的globalfifo设备驱动代码分析以及测试代码

    #include <linux/module.h> #include <linux/types.h> #include <linux/fs.h> #include ...

  6. 【Base64】JDK里面实现Base64的API

    原文出处: 成熟的毛毛虫的博客 BASE64 编码是一种常用的字符编码,在很多地方都会用到.但base64不是安全领域下的加密解密算法.能起到安全作用的效果很差,而且很容易破解,他核心作用应该是传输数 ...

  7. mvc razor页面的邮箱校验

    由于@符号是razor中的关键字,而邮箱校验的正则表达式中需要使用@符号,所以在cshtml页面的代码中直接写js代码进行邮箱校验会报错. 解决方案: 将邮箱校验写在js文件中,在cshtml文件中引 ...

  8. 我的PHP之旅--认识数据库及数据库操作

    数据库基本知识 什么是数据库服务器:安装了数据库软件的电脑,就是数据库服务器,同理安装了Apache软件的电脑就是www服务器. 什么是DBMS:database management system( ...

  9. android对大图片的缓存处理

    废话不多说,直接上代码 package com.huge.emj.common.util; import java.io.File; import java.io.FileInputStream; i ...

  10. SpringMVC与Struts2关于controller线程安全问题

    SpringMVC的controller是单例的,因此springMVC的controller不是线程安全的,在使用的时候要谨慎添加成员变量,因为所有的请求都会共享这个变量. 与springMVC不同 ...