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 ...
随机推荐
- HashMap的实现原理,以及在JDK1.7和1.8的区别
1.JDK1.7 HashMap是Java中大家最常用的一个map实现类,其为键值对也就是key-value的形式.他的数据结构则是采用的位桶和链表相结合的形式完成了,即拉链法.具体如下图所示: Ha ...
- win10 安全设置
风险程序: C:\Users\dong\Downloads\KMSTools_V18.06.2016_Xitongzhijia\KMSTools.exe 发起来源:C:\Windows\Syste ...
- django 验证码实现
django验证码的使用: 验证码的作用:用于人机识别. 验证码 ###验证码: def code_str(request): from PIL import Image from PIL impor ...
- JSON数据的解析和生成(C++)
安装 "JSON for Modern C++" $ brew tap nlohmann/json $ brew install nlohmann_json 安装之后将/usr/l ...
- 面向对象的css less 和sass
Css 初始化 reset.css 或者 normalise . Near.css兼容IE6以及现代浏览器. Oocss 也就是面向对象的css 面向对象是将cs ...
- ssh 报错Host key verification failed 或Ubuntu connect to serve 失败
ssh 报错Host key verification failed 或Ubuntu connect to serve 失败 通常是因为没有装ssh sudo apt-get install o ...
- 正确理解c和c ++的复杂类型声明
本文作者girlrong是网易广州社区的C语言版版主,这篇文章被选在精华区.很是不错,不敢独享!据说她乐于助人,虚心诚恳,颇受网友欢迎.只可惜现在已退隐江湖了.在最近学习C语言过程中,了解些前辈大牛的 ...
- python--第十二天总结(Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy)
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
- ORACLE查询内存溢出
首先我们来看一个带排序的查询,点击工具栏的显示包含实际的执行计划. 1 SELECT * FROM AdventureWorks2008R2.Person.Person WHERE FirstName ...
- [leetcode]70. Climbing Stairs爬楼梯
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...