题目

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


题解

这道题还是一种找组合的可能性,类似于wordbreakii。
这里想法是,用递归循环找子问题的方法,把母串按所有组合可能性拆分,如果是回文,就加进来,当层数为s的length时就有一个结果了。
这里需要判断是否为回文。
利用validPalindrome的思想很容易就写出来了(这里不需要判断大小写还有有没有别的字符)。 代码如下:
 1     public ArrayList<ArrayList<String>> partition(String s) {
 2         ArrayList<String> item = new ArrayList<String>();
 3         ArrayList<ArrayList<String>> res = new ArrayList<ArrayList<String>>();
 4         
 5         if(s==null||s.length()==0)
 6             return res;
 7         
 8         dfs(s,0,item,res);
 9         return res;
     }
     
     public void dfs(String s, int start, ArrayList<String> item, ArrayList<ArrayList<String>> res){
         if (start == s.length()){
             res.add(new ArrayList<String>(item));
             return;
         }
         
         for (int i = start; i < s.length(); i++) {
             String str = s.substring(start, i+1);
             if (isPalindrome(str)) {
                 item.add(str);
                 dfs(s, i+1, item, res);
                 item.remove(item.size() - 1);
             }
         }
     }
     
     
     public boolean isPalindrome(String s){
          int low = 0;
          int high = s.length()-1;
          while(low < high){
              if(s.charAt(low) != s.charAt(high))
                 return false;
              low++;
              high--;
          }
          return true;
     }
												

Palindrome Partitioning leetcode java的更多相关文章

  1. leetcode 132. Palindrome Partitioning II ----- java

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

  2. Palindrome Partitioning——LeetCode

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

  3. Palindrome Number leetcode java

    题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...

  4. Java for LeetCode 132 Palindrome Partitioning II

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

  5. Java for LeetCode 131 Palindrome Partitioning

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

  6. [Leetcode][JAVA] Palindrome Partitioning II

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

  7. Palindrome Partitioning II Leetcode java

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

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

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

  9. LeetCode: Palindrome Partitioning 解题报告

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

随机推荐

  1. 图的遍历 之 深搜dfs

    DFS 遍历 深度优先搜索是一个递归过程,有回退过程. 对一个无向连通图,在访问图中某一起始顶点u 后,由u 出发,访问它的某一邻接顶点v1:再从v1 出发,访问与v1 邻接但还没有访问过的顶点v2: ...

  2. 8.4 正睿暑期集训营 Day1

    目录 2018.8.4 正睿暑期集训营 Day1 A 数对子 B 逆序对 C 盖房子 考试代码 A B C 2018.8.4 正睿暑期集训营 Day1 时间:4.5h(实际) 期望得分:30+50+3 ...

  3. Android任务和返回栈完全解析(转)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/41087993 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工 ...

  4. KVM源代码解读:linux-3.17.4\arch\x86\include\asm\kvm_host.h

    /* * Kernel-based Virtual Machine driver for Linux * * This header defines architecture specific int ...

  5. bzoj 3285 离散对数解指数方程

    /************************************************************** Problem: 3285 User: idy002 Language: ...

  6. bzoj 4036 集合幂级数

    集合幂级数其实就是一种集合到数的映射,并且我们针对集合的一些操作(or  xor and specil or )为这种映射定义运算.其中一些东西可以通过某些手段将其复杂度降低. orz vfk /** ...

  7. RC4加密算法的原理及实现

    RC4于1987年提出,和DES算法一样.是一种对称加密算法,也就是说使用的密钥为单钥(或称为私钥). 但不同于DES的是.RC4不是对明文进行分组处理,而是字节流的方式依次加密明文中的每个字节.解密 ...

  8. Programming 2D Games 读书笔记(第六章)

      http://www.programming2dgames.com/chapter6.htm 示例一:Bounce 边界碰撞测试 velocity为移动的速度, 超过右边界,velocity.x为 ...

  9. Unity3D实践系列02,查看Scene窗口物体

    删除"Hierarchy"窗口中的"Directional Light". 把鼠标放在"Scene"窗口,滑动鼠标滚轮,可以对"S ...

  10. iOS6新特征:UICollectionView介绍-非常棒 -转

    传送门:http://www.devdiv.com/forum.php?mod=viewthread&tid=128378