LeetCode OJ--Palindrome Partitioning **
https://oj.leetcode.com/problems/palindrome-partitioning/
给定一个字符串 s,求所有的子串组合,每个子串都是回文的。
比如,aba: {a,b,a},{aba}
对于这类问题(对一个串的划分)基本上就是用递归。
首先设一个 record 数组,记录中间结果,省的多次计算。
class Solution {
public:
vector<vector<string> > partition(string s) {
vector<vector<string> > ans;
if(s.empty())
return ans;
const size_t len = s.size();
vector<vector<bool> > flag;
flag.resize(len);
for(int i = ;i<len;i++)
flag[i].resize(len);
for(int i = ;i<len;i++)
for(int j = i;j<len;j++)
{
flag[i][j] = isPal(s,i,j);
}
vector<string> ansPiece;
sub(,flag,ans,ansPiece,s);
return ans;
}
void sub(int begin,vector<vector<bool> > &flag,vector<vector<string> > &ans,vector<string> &ansPiece,string &s)
{
if(begin == s.size())
{
vector<string> _ansPiece = ansPiece;
ans.push_back(_ansPiece);
return ;
}
//here i means end position
for(int i = begin;i<flag.size();i++)
{
string tempstr;
if(flag[begin][i])
{
tempstr = s.substr(begin,i-begin+);
ansPiece.push_back(tempstr);
sub(i+,flag,ans,ansPiece,s);
ansPiece.pop_back();
}
}
}
bool isPal(string s,int i,int j)
{
if(i==j)
return true;
int temp = ;
while(i+temp<j-temp)
{
if(s[i+temp]!=s[j-temp])
return false;
temp++;
}
return true;
}
};
LeetCode OJ--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 ...
随机推荐
- Vue之简易的留言板功能
今天我将带大家通过Vue的todolist案例来完成一个简易的网页留言板! LES'T GO! 首先我们需要在页面上搭建一个input文本输入框,并设置提交按钮,通过循环指令来完成输入框的信息提交! ...
- Http状态码(了解)
一些常见的http状态码 200 - OK,服务器成功返回网页 - Standard response for successful HTTP requests. 301 - Moved Pe ...
- Redis实现之数据库(一)
服务器中的数据库 Redis服务器将所有数据库都保存在服务器状态redis.h/redisServer结构体的db数组中,db数组的每个项都是一个redis.h/redisDb结构体,每个redisD ...
- windows下虚拟环境中配置MySQL-python错误问题
下载mysql 下载mysql-python 这两步基本没有问题怪就怪的 MySQL-python-1.2.3.win-amd64-py2.7 文件只能安装到python27 路径下 然后在虚拟环境 ...
- Firewall Rule Properties Page: Advanced Tab
Applies To: Windows 7, Windows Server 2008 R2 Use this tab to configure the profiles and interface t ...
- 基于web自动化测试框架的设计与开发(本科论文word)
- Java线程池使用和源码分析
1.为什么使用线程池 在多线程编程中一项很重要的功能就是执行任务,而执行任务的方式有很多种,为什么一定需要使用线程池呢?下面我们使用Socket编程处理请求的功能,分别对每种执行任务的方式进行分析. ...
- POJ 2728 Desert King(最优比例生成树 二分 | Dinkelbach迭代法)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25310 Accepted: 7022 Desc ...
- GDOI2018前夕 错误总结
算法易错点 $FFT$ 1.注意精度,以及是否取整 2.注意$complex$类不要写错,复数乘法是这样的: complex operator *(const complex &b){retu ...
- Teleportation(tel)
Teleportation(tel) 题目描述 Zy大帝拥有n个星球,因为距离非常遥远,所以Zy在他所居住的1号星球和他的军事基地霸中所在的2号星球建造了两个传送门,这样从1号星球到2号星球就只需要2 ...