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. ubuntu下vim的简单配置

    该文章只是进行符合自己习惯的最基本的配置,更加高级的配置请参考更加有含量的博文! 1.打开vim下的配置文件 sudo vim /etc/vim/vimrc 2.在这个文件中,会有这么一句:synta ...

  2. angularjs的使用技巧

    1. AngularJS的module函数有两种用法, a. 定义一个module, 需要传入2个参数,module('moduleName', []), 第一个参数是新的module名称,第二个参数 ...

  3. Android 完美解决bundle实现页面跳转并保留之前数据+传值

    1.前言 前言: 昨天碰到了一个问题,我想实现页面跳转,采用了Bundle之后,再回到原来的页面,发现数据也没有了, 而且一直报错,网上查找了很多资料,发现要用一个startActivityForRe ...

  4. Python中str、list、numpy分片操作

    在Python里,像字符串(str).列表(list).元组(tupple)和这类序列类型都支持切片操作 对对象切片,s是一个字符串,可以通过类似数组索引的方式获取字符串中的字符,同时也可以用s[a: ...

  5. setTimeout相关整理

    setTimeout里面函数有无双引号的区别 双引号中的作用域不捕捉局部变量,不用双引号包着的是捕捉局部作用域 var a = function(){ alert(1111) } function a ...

  6. JS 小数处理

    parseInt(7/2);//丢弃小数部分,保留整数部分 Math.ceil(7/2);//向上取整 Math.floor(7/2);//向下取整 Math.round(7/2);//四舍五入 // ...

  7. 手机APP设计网

     http://hao.xueui.cn/ http://www.25xt.com/ 

  8. Eureka 简介以及简单示例(创建EurekaServer工程)

    Eureka 是一款开源的服务注册与发现组件,通过配合其他组件可提供负载均衡能力. 服务发现类型的技术对比: 名称 类型 AP/CP 语言 依赖 集成 一致性算法 Eureka General AP ...

  9. WebService常用公共接口

    Web Service 一些对外公开的网络服务接口   商业和贸易: 1.股票行情数据 WEB 服务(支持香港.深圳.上海基金.债券和股票:支持多股票同时查询) Endpoint: http://we ...

  10. eclipse删除svn下载的文件后如何恢复

    Team右键-->与资源库同步-->覆盖/更新