131.leetcode-Palindrome Partitioning
解法一.
class Solution {
public:
vector<vector<string>> partition(string s) {
vector<vector<string> > res;
vector<string> cur;
DFS(res, cur, s, );
return res;
}
void DFS(vector<vector<string> >& res, vector<string>& cur, string s, int start)
{
if(start >= s.size())
{
res.push_back(cur);
return ;
}
for(int i = start; i < s.size(); i++)
{
if(ispalindrome(s, start, i))
{
cur.push_back(s.substr(start, i-start+));
DFS(res, cur, s, i+);
cur.pop_back();
}
}
}
bool ispalindrome(string s, int start, int end)
{
while(start < end)
{
if(s[start++] != s[end--])
return false;
}
return true;
}
};
对于解法一,每次要求是否为回文串导致时间效率降低
解法二使用DP先求出回文串,然后直接DFS效率会高一点,可是空间效率会降低
class Solution {
public:
vector<vector<string>> partition(string s) {
vector<vector<string> > res;
vector<string> cur;
vector<vector<bool> > dp(s.size(), vector<bool>(s.size(), false));
getDP(dp, s);
DFS(res, cur, s, , dp);
return res;
}
void DFS(vector<vector<string> >& res, vector<string>& cur, string s, int start, vector<vector<bool> >& dp)
{
if(start >= s.size())
{
res.push_back(cur);
return ;
}
for(int i = start; i < s.size(); i++)
{
if(dp[start][i])
{
cur.push_back(s.substr(start, i-start+));
DFS(res, cur, s, i+, dp);
cur.pop_back();
}
}
}
void getDP(vector<vector<bool> >& dp, string s)
{
for(int i = ; i < dp.size(); i++)
{
for(int j = i, k = ; j < dp.size(); j++, k++)
{
if(abs(j-k) <= )
dp[k][j] = s[k] == s[j] ? true : false;
else
dp[k][j] = (dp[k+][j-])&&(s[k] == s[j]);
}
}
}
};
小白欢迎各位大神指点
131.leetcode-Palindrome Partitioning的更多相关文章
- 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@ [131/132] Palindrome Partitioning & Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...
- LeetCode(131)Palindrome Partitioning
题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- LeetCode: Palindrome Partitioning [131]
[称号] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- [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 ...
- 131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串
131. Palindrome Partitioning Given a string s, partition s such that every substring of the partitio ...
- [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 ...
随机推荐
- Java 学习 注解
转载:https://blog.csdn.net/briblue/article/details/73824058 注解语法 因为平常开发少见,相信有不少的人员会认为注解的地位不高.其实同 class ...
- py库:把python打包成exe文件(pyinstaller)
http://blog.csdn.net/be_quiet_endeavor/article/details/73929077 用Pyinstaller把Python3.4程序打包成可执行文件exe ...
- django之normalize函数的功能
from django.utils.regex_helper import normalize pat=r'^(?P<id>\d+)/(?P<name>\d+)$' bits= ...
- Django之Auth模块 实现登录,退出,自带session 与认证功能的一个重要的模块
Auth模板 1. 什么是Auth模块,有什么用? django的auth的模块的使用: auth 是集合注册,登录,注销,session 多个功能集合在一起的模块 2. 使用Auth组件的默认aut ...
- 深度学习中,使用regularization正则化(weight_decay)的好处,loss=nan
刚开始训练一个模型,自己就直接用了,而且感觉训练的数据量也挺大的,因此就没有使用正则化, 可能用的少的原因,我也就不用了,后面,训练到一定程度,accuracy不上升,loss不下降,老是出现loss ...
- sentinel 控制台接入
SpringBoot Web应用== 1. 引入sentinel依赖(你可以在maven仓库查找最新版,点击直接查看) sentinel别的依赖不用引入了,这个依赖基本全部引入了. <!--接 ...
- SQLLDR导入乱码问题的解决
SQLLDR导入乱码问题的解决 处理过程: 1.本地建立控制文件 load data infile 'd:\TMP_KAITOUSHUJU.csv' into table TMP_KAITOU ...
- c# NPOI 导出EXCEL (在下方显示图片)
需要引入dll文件 也可以在NuGet里面管理(推荐) 比较方便 . using System; using System.Collections.Generic; using System.Linq ...
- oracle 查询索引和主键
ORACLE: 1.查主键名称: select * from user_constraints where table_name = 'AAA' and constraint_type ='P'; 查 ...
- svn 清理失败的解决方法
首先 下载 SQLiteSpy 工具, 解压后如下图所示, 打开 .exe 应用程序,File ,opendatabase 选中wc.db 然后执行 delete from work_qu ...