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 **的更多相关文章

  1. [LeetCode] 131. Palindrome Partitioning 回文分割

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  2. [Leetcode Week13]Palindrome Partitioning

    Palindrome Partitioning 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/palindrome-partitioning/desc ...

  3. Leetcode 131. Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  4. 【leetcode】Palindrome Partitioning II(hard) ☆

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  5. [Leetcode][JAVA] Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  6. 【leetcode】Palindrome Partitioning II

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  7. 【leetcode】Palindrome Partitioning

    Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...

  8. leetcode 132. Palindrome Partitioning II ----- java

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  9. leetcode之 Palindrome Partitioning I&II

    1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...

  10. [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 ...

随机推荐

  1. 哦?原来Python 面试题是这样的,Python面试题No19

    本面试题题库,由公号:非本科程序员 整理发布 第1题:是否遇到过python的模块间循环引用的问题,如何避免它? 这是代码结构设计的问题,模块依赖和类依赖 如果老是觉得碰到循环引用可能的原因有几点: ...

  2. Django2.2使用mysql数据库pymysql版本不匹配问题的解决过程与总结

    前置条件 django版本:2.2.1 python版本:3.6.6 mysql版本:mysql-community8.0.15 问题 在搭建django项目,配置mysql数据库时遇到无法迁移数据库 ...

  3. Flask初学者:session操作

    cookie:是一种保存数据的格式,也可以看成是保存数据的一个“盒子”,服务器返回cookie给浏览器(由服务器产生),由浏览器保存在本地,下次再访问此服务器时浏览器就会自动将此cookie一起发送给 ...

  4. ajax提交表单,支持文件上传

    当我们提交表单但是又不想要刷新页面的时候就可以考虑使用ajax来实现提交功能,但是这有个局限就是当有文件上传的时候是行不通的,下面借助于jquery.form可以很方便满足我们的需求.   1.表单写 ...

  5. mof格式的文件怎么打开?用什么工具?

    托管对象格式 (MOF) 文件是创建和注册提供程序.事件类别和事件的简便方法.在 MOF 文件中创建类实例和类定义后,可以对该文件进行编译.有关更多信息,请参见编译托管对象格式 (MOF) 文件.编译 ...

  6. 史上最全的MSSQL笔记

    http://www.cnblogs.com/gameworld/archive/2015/09/08/4790881.html

  7. Oracle 学习笔记(Windows 环境下安装 + PL/SQL)

    Oracle 安装.PL/SQL 配置使用  前言:因更换机械硬盘为 SSD 固态硬盘装了新 Windows 7 系统,需要重新搭建开发环境,把 Oracle 安装过程和 PL/SQL 配置使用做下笔 ...

  8. MySQL出现错误1045,用户无法连接

    1.root用户可以登陆进去 假设新建了个sqlin用户,但是无法用此用户连接MySQL 这个时候root用户可以登录吧,root用户登录进去,重新修改该用户的密码,执行语句 use mysql; u ...

  9. python 学习分享-常用模块篇

    模块 就是前人给你造的轮子,你开车就好!!! 常用模块有: time模块 random模块 os模块 sys模块 shutil模块 json  &  picle模块 shelve模块 xml处 ...

  10. PHP FILTER_VALIDATE_IP 过滤器

    FILTER_VALIDATE_IP 过滤器把值作为 IP 进行验证,用于验证 IP 是否合法,支持 IPV6 验证 例子 <?php $ip = "192.168.0.1" ...