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. maven 启动忽略test

    两种方法 1,--命令 mvn install -Dmaven.test.skip=true 2.pom.xml 文件 在tomcat 下面的pom.xml 文件里面加上如下 <!--  ski ...

  2. 9.8 noip模拟试题

    LazyChild黑OJ(blackoj.pas/c/cpp) LazyChild开了一家“善良OJ”.但大多数人都不知道,这其实是家黑OJ.亲爱的同学,请不要惊讶,古时候有黑店,现代为什么不能有黑O ...

  3. codevs 1994 排队 排列组合+高精度

    /* 数学题0.0 最后答案:A(n,n)*A(n+1,2)*A(n+3,m)+A(n,n)*C(m,1)*A(2,2)*C(n+1,1)*A(n+2,m-1); 简单解释一下 +之前的很显然 先排男 ...

  4. web-请求无缓存

    <head><META HTTP-EQUIV="pragma" CONTENT="no-cache"><META HTTP-EQU ...

  5. NSString截取字符串

     NSString 是经常会用到的,很多时候需要对字符串进行一些处理,本文简单介绍字符串截取操作: 比如: 1.定义一个字符串a, 截取a的某一个部分(子串) NSString *a = @" ...

  6. GCD多线程 在子线程中获取网络图片 在主线程更新

    子线程中得所有数据都可以直接拿到主线程中使用 //当触摸屏幕的时候,从网络上下载一张图片到控制器的view上显示 -(void)touchesBegan:(NSSet *)touches withEv ...

  7. iOS开发中常用的宏

    前言 今天将一些简化工程代码的宏定义拿出来分享一下,自定义一些宏可以有效的简化代码,提高编码效率. Application #define APPLICATION [UIApplication sha ...

  8. CompareValidator ASP控件

    定义和用法 CompareValidator 控件用于将由用户输入到输入控件的值与输入到其他输入控件的值或常数值进行比较. 注释:如果输入控件为空,则不会调用任何验证函数,并且验证将成功.使用 Req ...

  9. centos 用户切换

    在系统的/etc/.bash_profile中已经配置了各种环境变量. 用账户a登陆,ldd xxx.so查看一切链接正常. 用账户root登陆,ldd xxx.so查看一切链接正常. 用账户a登陆, ...

  10. 『重构--改善既有代码的设计』读书笔记----Extract Method

    在编程中,比较忌讳的一件事情就是长函数.因为长函数代表了你这段代码不能很好的复用以及内部可能出现很多别的地方的重复代码,而且这段长函数内部的处理逻辑你也不能很好的看清楚.因此,今天重构第一个手法就是处 ...