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 ...
随机推荐
- HTML - input(转)
自: http://www.runoob.com 标签定义及使用说明 <input> 标签规定了用户可以在其中输入数据的输入字段. <input> 元素在 <form&g ...
- 去“BAT”这样面试,拿到offer的几率是80%(转)
一.概述面试,难还是不难?取决于面试者的底蕴(气场+技能).心态和认知及沟通技巧.面试其实可以理解为一场聊天和谈判,在这过程中有心理.思想上的碰撞和博弈.其实你只需要搞清楚一个逻辑:“面试官为什么会这 ...
- java 自动补全
int youNumber = 1; // 0 代表前面补充0 // 4 代表长度为4 // d 代表参数为正数型 String str = String.format("%04d" ...
- Mybatis控制台打印sql
mybatis-config.xml配置如下: <configuration> <settings> <setting name="lazyLoadingEna ...
- FlannBasedMatcher 立体匹配
#include "opencv2/core/core.hpp" #include "opencv2/features2d/features2d.hpp" #i ...
- CAFE: a computational tool for the study of gene family evolution
1.摘要 摘要:我们提出了CAFE(计算分析基因家族进化),这是一个统计分析基因家族进化规模的工具.它使用随机的出生和死亡过程来模拟一个系统发育过程中基因家族大小的进化.对于一个特定的系统发育树,并给 ...
- Linux shell read 解析
read是一个重要的bash命令,它用于从键盘或标准输入中读取文本,我们可以用read以交互的方式读取来自用户的输入,不过read能做的可远不止这些,当从键盘读取用户输入的时候,只有按下回车键才标志输 ...
- Java 基本类型和包装类型
讲基本类型和包装类型之前,首先要介绍,装箱和拆箱 装箱:基本类型转化为包装类型 拆箱:包装类型转化为拆箱类型 为什么要有包装类型?Java是面向对象的语言,Java中一切都是对象除了基本数据类型,所以 ...
- 890. Find and Replace Pattern找出匹配形式的单词
[抄题]: You have a list of words and a pattern, and you want to know which words in words matches the ...
- [leetcode]21. Merge Two Sorted Lists合并两个链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...