题目

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. jquery获取系统当前时间

    //判断是否在前面加0function getNow(s) { return s < 10 ? '0' + s: s;} var myDate = new Date(); var year=my ...

  2. hdu CA Loves GCD(dp)

    一道我想骂人的题,差点把我气炸了. 题意: 求一个数的集合中(非多重集,每个数只出现一次)所有子集的gcd的和.结果MOD10^8+7输出. 输入输出不说了,自己看吧,不想写了. 当时我真把它当作数论 ...

  3. LPC43xx SGPIO Experimentation

    LPC4350 SGPIO Experimentation The NXP LPC43xx microcontrollers have an interesting, programmable ser ...

  4. linux 内核升级2 转

    linux内核升级 一.Linux内核概览 Linux是一个一体化内核(monolithic kernel)系统. 设备驱动程序可以完全访问硬件. Linux内的设备驱动程序可以方便地以模块化(mod ...

  5. solaris 软件包地址

      1. http://www.opencsw.org/ 在Solaris 10下 1.安装pkgutil pkgadd -d http://get.opencsw.org/now 2.查询有那些PK ...

  6. Linux网络编程socket选项之SO_LINGER,SO_REUSEADDR

    from http://blog.csdn.net/feiyinzilgd/article/details/5894300 Linux网络编程中,socket的选项很多.其中几个比较重要的选项有:SO ...

  7. Revit API创建墙的保温层修改墙厚度

    start [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)]  / ;         ; ...

  8. 什么是K线?K线的详解!

    K线图这种图表源于日本德川幕府时代,被当时日本米市的商人用来记录米市的行情与价格波动,后因其细腻独到的标画方式而被引入到股市及期货市场.目前,这种图表分析法在我国乃至整个东南亚地区均尤为流行.由于绘制 ...

  9. firedac调用ORACLE的存储过程

    firedac调用ORACLE的存储过程 EMB官方原文地址:http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Using_Oracle_with_F ...

  10. PPT幻灯片放映不显示备注,只让备注显示在自己屏幕上-投影机 设置

    无论是老师或是讲师还是即将要演讲的人,在讲课之前一定会做好课件,到哪一页该讲哪些内容,到哪里该如何去讲等等.那么一般的讲师会将这些课件存放到哪里呢?是用个书本记载下来呢,还是直接存放到电脑上呢?其实本 ...