Palindrome Partitioning 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/palindrome-partitioning/description/


Description

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

Return all possible palindrome partitioning of s.

For example, given s = "aab",

Return

[
["aa","b"],
["a","a","b"]
]

Solution

class Solution {
public:
vector<vector<string>> partition(string s) {
int len = s.length();
vector<vector<string>> res;
vector<string> path;
dfs(0, s, path, res);
return res;
} void dfs(int idx, string& str, vector<string>& path, vector<vector<string>>& res) {
if (idx == str.length()) {
res.push_back(path);
return;
}
for (int i = idx; i < str.size(); i++) {
if (isPalindrome(str, idx, i)) {
path.push_back(str.substr(idx, i - idx + 1));
dfs(i + 1, str, path, res); /* pop back every time in recurse stack,
* than all the paces added in dfs can be remove */
path.pop_back();
}
}
} bool isPalindrome(string& str, int start, int end) {
while (start < end) {
if (str[start] != str[end]) {
return false;
}
start++;
end--;
}
return true;
}
};

解题描述

这道题是目的是找到一个字符串中所有由回文子串组成的集合,算法是对给出的字符串进行遍历,查找所有回文子串,对每个回文子串再进行DFS查找新的回文子串,这样就能找到所有由回文子串切分的子串的集合。

[Leetcode Week13]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 131. Palindrome Partitioning

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

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

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

  4. [Leetcode][JAVA] Palindrome Partitioning II

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

  5. 【leetcode】Palindrome Partitioning II

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

  6. 【leetcode】Palindrome Partitioning

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

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

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

  8. leetcode之 Palindrome Partitioning I&II

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

  9. [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. AngularJS 学习笔记--01

    学习 AngularJS 要先了解 MVC 模式 , 即 " 模型--视图--控制器 " . 模型: 包含了需要用到的数据 ; 有两种广义上的模型 : 视图模型 , 只表示从控制器 ...

  2. [计算机网络-传输层] 无连接传输:UDP

    UDP(用户数据报协议) 下面是UDP的报文段格式: 可以看出UDP的首部长度是固定的,共64bit,即8个字节. 校验和:提供了差错检测得功能,即用于确定当UDP报文段从源到达目的时,其中的比特是否 ...

  3. Activiti5工作流笔记四

    排他网关(ExclusiveGateWay) 流程图 部署流程定义+启动流程实例 查询我的个人任务 完成我的个人任务 并行网关(parallelGateWay) 流程图 部署流程定义+启动流程实例 查 ...

  4. bzoj4332[JSOI2012]分零食

    一下午被这题的精度续掉了...首先可以找出一个多项式的等比数列的形式,然后类似poj的Matrix Series,不断倍增就可以了.用复数点值表示进行多次的多项式运算会刷刷地炸精度...应当用int存 ...

  5. 关于 [lambda x: x*i for i in range(4)] 理解

    题目: lst = [lambda x: x*i for i in range(4)] res = [m(2) for m in lst] print res 实际输出:[6, 6, 6, 6] 想要 ...

  6. hadoop 使用Avro求最大值

    在上例中:hadoop MapReduce辅助排序解析,为了求每年的最大数据使用了mapreduce辅助排序的方法. 本例中介绍利用Avro这个序列化框架的mapreduce功能来实现求取最大值.Av ...

  7. POJ 3261 Milk Patterns (后缀数组,求可重叠的k次最长重复子串)

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 16742   Accepted: 7390 Ca ...

  8. 【题解】JXOI2017颜色

    一眼线段树...显然,我们可以考虑最后所留下的区间,那显然这个区间中应当不能存在任何与区间外相同的颜色.这里的转化也是很常用的,我们用 \(nxt[i]\) 表示与 \(i\) 颜色相同的下一个位置在 ...

  9. c++【1】

    高考完了,重新做码农.要转c++. 按照粉书什么的.果然要告别p党了呢. 第一部分 语言篇 第一章:程序输入 主要是一些输入输出格式. #include<cstdio> 输出: print ...

  10. [NOIP2016] 天天爱跑步 桶 + DFS

    ---题面--- 题解: 很久以前就想写了,一直没敢做,,,不过今天写完没怎么调就过了还是很开心的. 首先我们观察到跑步的人数是很多的,要一条一条的遍历显然是无法承受的,因此我们要考虑更加优美的方法. ...