给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。
返回 s 所有可能的分割方案。
例如,给出 s = "aab",
返回
[
  ["aa","b"],
  ["a","a","b"]
]
详见:https://leetcode.com/problems/palindrome-partitioning/description/

Java实现:

  1. class Solution {
  2. public List<List<String>> partition(String s) {
  3. List<List<String>> res=new ArrayList<List<String>>();
  4. if(s.isEmpty()){
  5. return res;
  6. }
  7. helper(s,0,new ArrayList<String>(),res);
  8. return res;
  9. }
  10. private void helper(String s,int start,List<String> out,List<List<String>> res){
  11. if(start==s.length()){
  12. res.add(new ArrayList<String>(out));
  13. return;
  14. }
  15. for(int i=start;i<s.length();++i){
  16. if(isPalindrome(s,start,i)){
  17. out.add(s.substring(start,i+1));
  18. helper(s,i+1,out,res);
  19. out.remove(out.size()-1);
  20. }
  21. }
  22. }
  23. private boolean isPalindrome(String s,int start,int end){
  24. while(start<end){
  25. if(s.charAt(start)!=s.charAt(end)){
  26. return false;
  27. }
  28. ++start;
  29. --end;
  30. }
  31. return true;
  32. }
  33. }

131 Palindrome Partitioning 分割回文串的更多相关文章

  1. lintcode:Palindrome Partitioning 分割回文串

    题目: 分割回文串 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa&q ...

  2. Leetcode131. Palindrome Partitioning分割回文串

    给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa",&quo ...

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

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

  4. Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)

    Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning) 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. ...

  5. LeetCode 131. 分割回文串(Palindrome Partitioning)

    131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...

  6. 分割回文串 · Palindrome Partitioning

    [抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 给出 s = "aab",返回 [ ["aa", & ...

  7. Java实现 LeetCode 131 分割回文串

    131. 分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa ...

  8. Leetcode 131.分割回文串

    分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa" ...

  9. [LeetCode] 132. 分割回文串 II

    题目链接 : https://leetcode-cn.com/problems/palindrome-partitioning-ii/ 题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子 ...

随机推荐

  1. NullpointerException真的一定要被预防?

    毫无疑问,空指针NullpointerException是我们最常遇到异常,没有之一! 在刚进入编程职业时,我想,大部分进入的同学肯定会受到前辈们的叮咛:一定要防止空指针,这是个低级错误.你们不是?好 ...

  2. hibernate 的分页查询

    hibernate的分页查询有个好处,就是不用管数据库方言.比如db2的分页查询很麻烦,但是用hibernate的方式,就完全不用管这些了 /* 使用HQL分页查询Customer信息 */ publ ...

  3. [原创] Karen and Supermarket 2

    在回家的路上,凯伦决定到超市停下来买一些杂货. 她需要买很多东西,但因为她是学生,所以她的预算仍然很有限. 事实上,她只花了b美元. 超市出售N种商品.第i件商品可以以ci美元的价格购买.当然,每件商 ...

  4. java map 装入list

    需要生成多组数据的时候,应将map时候放入循环,否则循环出来会一直覆盖之前的,只能保存一条数据. 具体如下: if (rs.next()) { do { Map<String, String&g ...

  5. linux网络配置及IP绑定

    在学习时,参考了这篇文章:http://blog.csdn.net/collection4u/article/details/14127671:在这篇文章中作者讲述了VMware中虚机的三种网络模式: ...

  6. UVA10480 Sabotage —— 最小割最大流

    题目链接:https://vjudge.net/problem/UVA-10480 题解: 实际就是求最小割集. 1.什么是网络流图的“割”?答:一个边的集合,使得网络流图删除这些边之后,点被分成两部 ...

  7. 项目中Redis分库

    Redis中有16个库 默认第0个库 配置库的设置: 不同的库  key可以重复哈 公司的多个不同分布式项目,但是只有一个redis时候,以项目方式区分不同的库 每个项目连接相同 但是库不同

  8. Do not throw System.Exception, System.SystemException, System.NullReferenceException, or System.IndexOutOfRangeException intentionally from your own source code

    sonarqube的扫描结果提示 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/exceptions/creatin ...

  9. UVA-11078(水题)

    题意: 给一个序列,找两个整数a[i],a[j]使得a[i]-a[j]最大; 思路: 从前往后扫一遍;水题; AC代码: #include <bits/stdc++.h> /* #incl ...

  10. GYM 100741A Queries

    传送门 题目大意: 一个长度为n的序列,q次三种操作 +p  r:下标为p的数+r -p r:下标为p的数-r s l r mod [L,R]中有多少数%m=mod,m已经给出 题解: 开十个树状数组 ...