Palindrome Partitioning

Total Accepted: 21056 Total
Submissions: 81036My Submissions

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"]
]

题意:切割字符串,使每一个子串都是回文

思路:dfs

选择一个切割点时,假设从起点到切割点的子串是回文,那么该切割点是合法的。能够选择

按这个规则dfs枚举就能够了

复杂度:时间O(n)。空间O(log n)

vector<vector<string> >res;

bool is_palindrome(const string &s, int start, int end){
while(start < end){
if(s[start] != s[end - 1]) return false;
++start; --end;
}
return true;
} void dfs(const string &s, int cur, vector<string>& partitions){
int size = s.size();
if(cur == size){
res.push_back(partitions);
}
for(int end = cur + 1; end <= size; ++end){
if(is_palindrome(s, cur, end)){
partitions.push_back(s.substr(cur, end - cur));
dfs(s, end, partitions);
partitions.pop_back();
}
}
} vector<vector<string>> partition(string s) {
vector<string> tem;
dfs(s, 0, tem);
return res;
}

leetcode dfs 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 I&II

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

  5. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  6. 【算法】leetcode之 Palindrome Partitioning I&II(转载)

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

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

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

  8. [Leetcode][JAVA] Palindrome Partitioning II

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

  9. 【leetcode】Palindrome Partitioning II

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

随机推荐

  1. bzoj4956: [Wf2017]Secret Chamber at Mount Rushmore

    F......loyd? 这范围也是..... 然而大了也不会.... #include<cstdio> #include<iostream> #include<cstr ...

  2. POJ3414 Pots

    题目: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i)        将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DROP(i)        将第i个容器抽干 ...

  3. 20. Valid Parentheses[E]有效的括号

    题目 Given a string containing just the characters '(',')','[',']','{' and '}',determine if the input ...

  4. C# List<T>转成DataTable

    //将List<T>转成DataTable         public static DataTable ToDataTable(List<T> collection)    ...

  5. .net几种文件下载的方法

    .Net文件下载方式.... 之前在写上传文件.下载文件的时候,发现Response对象里面有好几种下载文件的方式,之后自己亲自实践了这几种方法,记录下以便以后复习... WriteFile文件下载 ...

  6. 如何用npm安装vue

    下载安装node.js,安装完毕做好配置后开始运行. 如果npm版本太低,需要升级一下npm # 查看版本 $ npm -v #升级 npm cnpm install npm -g 安装vue-cli ...

  7. js去除html标记

    function ff(str) { var dd = str.replace(/<\/?.+?>/g, ""); var dds = dd.replace(/ /g, ...

  8. Django使用中常见的错误

    Django Mysql SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED   Django 执行makemigrations  的时候报错 ...

  9. BZOJ 3456: 城市规划 多项式求逆

    Description 刚刚解决完电力网络的问题, 阿狸又被领导的任务给难住了.  刚才说过, 阿狸的国家有n个城市, 现在国家需要在某些城市对之间建立一些贸易路线, 使得整个国家的任意两个城市都直接 ...

  10. SQL SEVER 递归查询

    with ts as ( --首先要查询出最原始父级的信息 union all --全连接 select a.fitemclassid,a.fitemid, a.fnumber,a.Fparentid ...