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. 利用Powershell和ceye.io实现Windows账户密码回传

    利用Powershell和ceye.io实现Windows账户密码回传 转自:http://www.freebuf.com/articles/system/129068.html 最近在研究Power ...

  2. CodeForces 651C

    Description Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg s ...

  3. php Aes 128位算法

    <?php class Mcrypt { private static $key = "fsdjfojojodjiovjojgfosdjfiojio"; private st ...

  4. Elasticsearch日志收集

    Install pip if necessary curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py&q ...

  5. Arduino-OLED-四度显示

    double Fahrenheit(double celsius) { ; } //摄氏温度度转化为华氏温度 double Kelvin(double celsius) { return celsiu ...

  6. spring-boot结合mybatis-spring的一个例子

    首先spring-boot是用于简化配置的,具有可拔式组件的运用特点. 然后一下是spring-boot结合mybatis-spring的一个例子. 是一个maven项目 demo下载:http:// ...

  7. RAR 5.50 控制台使用记录

    copy from  WinRAR用户手册,备忘 用户手册 ~~~~~~~~ RAR 5.50 控制台版本 ~~~~~~~~~~~~~~~~~~~ =-=-=-=-=-=-=-=-=-=-=-=-=- ...

  8. Singleton pattern的线程安全问题

    original post from here方法一:同步机制关键词public class Singleton { 2 //利用静态变量来记录Singleton的唯一实例 3 private sta ...

  9. Goldengate升级之目标端(replicat端)升级

    转自红黑联盟Goldengate升级之目标端(replicat端升级 要升级replicat端的原因为:目标端OGG软件版本与源端OGG软件版本不同,在实际生产应用中,经常发现replicat端事务丢 ...

  10. (转)ORA-01502

    问题:ora-01502 索引或这类索引的分区处于不可用状态 引发:移动数据表分区,导致索引失效 解决:重建失效索引 1. select index_name ,status  from user_i ...