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 ...
随机推荐
- 动态规划:HDU-2955-0-1背包问题:Robberies
解题心得: 这题涉及概率问题,所以要运用概率的知识进行解答.题目要求不被抓到的概率,但是给出的是被抓到的概率,所要用1减去后得到答案.最好使用double类型,避免精度问题导致WA. 先算出可以抢劫的 ...
- Reachability from the Capital
题目描述 There are nn cities and mm roads in Berland. Each road connects a pair of cities. The roads in ...
- 给B公司的一些建议(又一篇烂尾的文章)
感慨:太多太多的悲伤故事,发生在自己身上,发生在自己的身边.因此,为了避免总是走"弯路",走"错误"的道路,最近一直在完善自己的理论模型. 烂尾说明:本文是一篇 ...
- 「微信小程序免费辅导教程」25,基本内容组件text的使用及个人帐号允许的服务类目
- HDU 4919 Exclusive or 数学
题意: 定义 \[f(n)=\sum\limits_{i=1}^{n-1}(i\oplus (n-i))\] 求\(f(n),n \leq 10^{500}\) 分析: 这个数列对应OEIS的A006 ...
- oracle JOB 查询 添加 修改 删除
-------------查询JOB----------------- select job, what, next_date, next_sec, sysdate, failures, broken ...
- C#入门篇6-2:字符串操作 string常用的函数
//String 字符串的常见操作 public static void Fun1() { string MyStr = " Hello World! "; //length长度属 ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- jeakins+maven+jmeter构建性能测试自动化( 在eclipse里运行如果出现没有找到“*.loadtest.xls”,请将此文件名修改为你对应使用的xsl文件名)
背景: 首先用jmeter录制或者书写性能测试的脚本,用maven添加相关依赖,把性能测试的代码提交到github,在jenkins配置git下载性能测试的代码,配置运行脚本和测试报告,配置运行失败自 ...
- Java类和对象 详解(一)---写的很好通俗易懂---https://blog.csdn.net/wei_zhi/article/details/52745268
https://blog.csdn.net/wei_zhi/article/details/52745268