leetcode之Palindrome Partitioning
方法一: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的更多相关文章
- [LeetCode] 131. Palindrome Partitioning 回文分割
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [Leetcode Week13]Palindrome Partitioning
Palindrome Partitioning 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/palindrome-partitioning/desc ...
- Leetcode 131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 【leetcode】Palindrome Partitioning II(hard) ☆
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [Leetcode][JAVA] 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
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【leetcode】Palindrome Partitioning
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- leetcode 132. Palindrome Partitioning II ----- java
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- leetcode之 Palindrome Partitioning I&II
1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...
- [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 ...
随机推荐
- 51nod1089最长回文子串V2
1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字 ...
- 要成为开发中最牛逼的测试,测试中最牛逼的开发。从今天起学python,写博客。--python基础学习(一)
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32Type & ...
- UI设计的奥义
个人觉得一个好的UI应该具备如下特点 1.符合人类认知行为 2.契合人体生物学 3.平滑,流畅 4.适当的交互会让你的应用更加成功 5.动态的内容才是招蜂引蝶的资本
- map遍历的三种基础用法
java中遍历MAP的几种方法 Java代码 Map<String,String> map=new HashMap<String,String>(); map.put(& ...
- Vijos P1062 迎春舞会之交谊舞
题目链接:https://vijos.org/p/1062 题意:输入n(n <= 1500)个女生左边有多少个男生.每个女生都和她左边最近的男生跳舞. 输出每个女生到可以与之跳舞的男生之间有几 ...
- iOS序列化与反序列化
1到底这个序列化有啥作用? 面向对象的程序在运行的时候会创建一个复杂的对象图,经常要以二进制的方法序列化这个对象图,这个过程叫做Archiving. 二进制流可以通过网络或写入文件中(来源于某教材的一 ...
- 30 个最棒的 jQuery 的拖放插件
jQuery 允许用户为任意 DOM 元素添加可拖放的功能,通过 jQuery 的拖放插件你可以轻松实现网页上任意元素的拖拽操作.在本文中我们向你推荐 30 个最棒的 jQuery 的拖放插件. 点击 ...
- 虚拟机重复创建系统去除SID
我们安装完的操作系统都会有一个SID,为了简化安装,现在大部分人会选择GHOST克隆安装,经过克隆后的系统SID是相同的,有时需要重新获取SID 以前WIN2003有修改SID的工具NEWSID ...
- Linux 系统挂载数据盘
适用系统:Linux(Redhat , CentOS,Debian,Ubuntu) * Linux的云服务器数据盘未做分区和格式化,可以根据以下步骤进行分区以及格式化操作. 下面的操作将会把数据盘划 ...
- maven 项目编译时候提示:Error building POM (may not be this project's POM).
编译时候提示Error building POM (may not be this project's POM)的错误,具体信息如下: [0] 'dependencies.dependency.ver ...