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 ...
随机推荐
- Maven CXF wsdl2Java 给指定名空间设置包名
<plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin< ...
- python读取excel表
from xlrd import open_workbookimport re #创建一个用于读取sheet的生成器,依次生成每行数据,row_count 用于指定读取多少行, col_count 指 ...
- 来自Github的优秀源码(python操作iframe框架网页)
#Please use your username and password for academia in codeimport timefrom selenium import webdriver ...
- webpack 应用笔记
1.https://segmentfault.com/a/1190000006178770 2. 组件介绍 01.webpack.prod.conf.js 在生产时 new webpack.optim ...
- Codeforces Round #437 C. Ordering Pizza
题意: n个人吃披萨,总共有两种披萨,每种披萨都是有S块,给出每个人要吃的块数,吃第一种披萨能获得的happy值,吃第二种披萨能获得的happy值,问你,在购买的披萨数最少的情况下能获得的最大的总的h ...
- springcloud-知识点总结(二):Ribbon&Feign
1.Ribbon简介 前面讲了eureka服务注册与发现,但是结合eureka集群的服务调用没讲. 这里的话 就要用到Ribbon,结合eureka,来实现服务的调用: Ribbon是Netflix发 ...
- VideoView 监听视频格式不支持时的错误。
视频播放格式不支持的处理https://www.cnblogs.com/ygj0930/p/7737209.html 不处理的情况下,默认会有弹框提示:不支持该视频格式. mVideoView.set ...
- JS获取URL中文参数乱码的解决方法
浏览器URL参数值中带有汉字字符,在接收时直接获取会出现乱码,下面是解决方法(传递前不需要encodeURI): function getUrlVars() { var vars = [], hash ...
- python基础 ---- 使用pyCharm 调试
debug -- 为了分析程序的异常 单步调试 1.设置断点 2.debug.启动 3.监控变量
- EasyWeChat使用(laravel框架下)
最近做了个项目是关于微信网页开发的,今天记录下在做项目中的关于微信这块遇到的一些坑 关于微信这块,用的是EasyWeChat,提高了开发的效率.在看EasyWeChat这个文档的时候发现了有专门针对l ...