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之后要还原状态,从上一个合法解之后继续遍历其他可能。

Talk is cheap>>

public class PalindromePartitioning {
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
ArrayList<String> tmp = new ArrayList<>();
int length = s.length();
dfs(s, tmp, res, length);
return res;
} public void dfs(String src, ArrayList<String> tmp, List<List<String>> res, int length) {
if (length == 0) {
res.add((ArrayList<String>) tmp.clone());
return;
}
for (int i = 1; i <= src.length(); i++) {
if (isValid(src.substring(0, i))) {
tmp.add(src.substring(0, i));
dfs(src.substring(i, src.length()), tmp, res, length - i);
tmp.remove(tmp.size() - 1);
}
}
} public boolean isValid(String s) {
if (s == null || s.length() <= 1) {
return true;
}
int i = 0, j = s.length() - 1;
while (i < j) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
}
 

Palindrome Partitioning——LeetCode的更多相关文章

  1. Palindrome Partitioning leetcode java

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

  2. [LeetCode] Palindrome Partitioning II 拆分回文串之二

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

  3. [LeetCode] Palindrome Partitioning 拆分回文串

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

  4. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  5. leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...

  6. [LeetCode] Palindrome Partitioning II 解题笔记

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

  7. [leetcode]Palindrome Partitioning II @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s  ...

  8. [leetcode]Palindrome Partitioning @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/ 题意: Given a string s, partition s suc ...

  9. LeetCode: Palindrome Partitioning 解题报告

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

随机推荐

  1. 给tcpdump加点颜色看看

    http://blog.csdn.net/voidccc/article/details/38797685

  2. mysql 大表 Sharding [转]

    参看以下两篇文章 http://www.dedecms.com/knowledge/data-base/mysql/2012/0820/9172.html http://dbanotes.net/da ...

  3. redis 多实例配置

    (redis的安装, 配置, 登陆等基础不再多说, 网上很多资料的, 这里只说个人对redis多实例的理解与配置) 我自己使用的redis版本是 2.8.13, 环境是 ubuntu 个人对多实例的理 ...

  4. 新一代 PHP 加速插件 Zend Opcache <转>

    注: 由于原链接已不存在, 所以我把图片重新整理了一下, 以便看起来更加直观 笔者注: 1>  PHP 性能提升之 PHP NG  =>  php next generation wiki ...

  5. checkbox 删除

    先创建del.php文件: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...

  6. Lucene全文检索

    Lucene写入和更新操作: if (id.equals("")) { this.goodsService.save(goods); String goods_lucene_pat ...

  7. ubuntu wine卸载程序并删除图标

    卸载ubuntu 下用wine安装的程序,可以用wine uninstaller命令,打开 添加/删除程序界面,进行删除程序操作:

  8. C#—集合(Collection)

    1.栈(stack<T>) using System; using System.Collections.Generic; using System.Linq; using System. ...

  9. Asp.net中的页面跳转及post数据

    /// <summary> /// This method prepares an Html form which holds all data /// in hidden field i ...

  10. iOS中常用的四种数据持久化技术

    iOS中的数据持久化方式,基本上有以下四种:属性列表 对象归档 SQLite3和Core Data 1.属性列表涉及到的主要类:NSUserDefaults,一般 [NSUserDefaults st ...