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. echarts 画 canvas 如果在IE8下不显示图标

    网上说法很多,可能版本问题,也有说script标签位置问题(放在body中) 不过先试已下,清除option对象中多余的逗号(,) 对象的最后一个属性后不要有逗号结尾

  2. MATLAN中矩阵表示中有一维是~表示的意思

    [~,m]表示的意思解释. [~,m]=rat(12/34) m = 17 >> [x,m]=rat(12/34) x = 6 m = 17

  3. codeforces 981 C.Useful Decomposition

    C. Useful Decomposition time limit per test 1 second memory limit per test 256 megabytes input stand ...

  4. 【bzoj2318】Spoj4060 game with probability Problem 概率dp

    题目描述 Alice和Bob在玩一个游戏.有n个石子在这里,Alice和Bob轮流投掷硬币,如果正面朝上,则从n个石子中取出一个石子,否则不做任何事.取到最后一颗石子的人胜利.Alice在投掷硬币时有 ...

  5. P1278 单词游戏

    题目描述 Io和Ao在玩一个单词游戏. 他们轮流说出一个仅包含元音字母的单词,并且后一个单词的第一个字母必须与前一个单词的最后一个字母一致. 游戏可以从任何一个单词开始. 任何单词禁止说两遍,游戏中只 ...

  6. CF995A Tesla

    题目描述 Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He kno ...

  7. 转:DP和HDP

    Dirichlet Process and Hierarchical Dirichlet Process 原文:http://hi.baidu.com/zentopus/item/46a622f5ef ...

  8. BZOJ2693:JZPTAP——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2693 Description   Input 一个正整数T表示数据组数 接下来T行 每行两个正整数 ...

  9. HDU5446:Unknown Treasure——题解

    http://acm.hdu.edu.cn/showproblem.php?pid=5446 求C(n,m)%(p1p2…pk)的值,其中pi均为质数. 参考:https://www.cnblogs. ...

  10. Json对象转为实体对象

    Json对象转为实体对象 1.Bean中申明 trainTypeList: public class TrainTypeQueryParam implements Serializable { pri ...