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 ...
随机推荐
- Django API 为 D3 提供数据
在工作中见过有的人即便使用了Django,依然还在采取json或geojson的文件形式为页面提供数据,相当于嵌入数据而非加载.下面是个简单有效的例子: 先从 model.py 开始 # models ...
- 51nod 1202 不同子序列个数(计数DP)
1202 子序列个数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 子序列的定义:对于一个序列a=a[1],a[2],......a[n].则非空序列a'=a[p1],a ...
- CCPC_1003
这个题可以暴力的哟,直接暴力的哟 不用做什么订立的哟 不需要特别判断的哟 去死吧!!!愚蠢的我! #include<bits/stdc++.h> using namespace std; ...
- Android百度地图开发 百度地图得到当前位置
1.申请key 2.复制jar,以及.so .注意要Libs目录右键build path -> use as source folder(这是一个坑) 3. AndroidMainFast.xm ...
- volley框架使用
volley网络请求一个json数据很简单,一句话就搞定了. StringRequest stringRequest=new StringRequest(url, new Listener<St ...
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 19: ordinal not in range(128)
解决方案: 1: 在网上找到的解决方案是: 在调用import matplotlib.pyplot as plt前 import sys sys.setdefaultencoding(“gbk”) 让 ...
- laravel5.2总结--ORM模型
ORM模型简介 1>什么是ORM? ORM,即 Object-Relational Mapping(对象关系映射),它的作用是在关系型数据库和业务实体对象之间作一个映射,这样,我们在操作具体的 ...
- Windows核心编程小结1
这本书绝对经典,看看定会增加不少知识.当然这本书有很多东西比<Windows程序设计第五版>中的更加详细. 1.Unicode:宽字节字符集 这是一个国际的字符标准,16位,最大可支持65 ...
- RT-Thread学习之——静态线程和动态线程
RT-Thread中支持静态和动态两种定义方式. 用线程来举例的话,rt_thread_init对应静态定义方式,rt_thread_create对应动态定义方式. 使用静态定义方式时,必须先定义静态 ...
- OtherStream
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataInputStream; import ...