题目

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. 闭包应用之延迟函数setTimeout

    根据HTML 5标准,setTimeout推迟执行的时间,最少是5毫秒.如果小于这个值,会被自动增加到5ms. 每一个setTimeout在执行时,会返回一个唯一ID,把该ID保存在一个变量中,并传入 ...

  2. python3.5 自带的虚拟环境使用

    首先我们要选择一个目录作为虚拟环境的目录, 这里选择c:\myenv cd myenv python -m venv . #在当前目录下创建虚拟环境 创建完成之后,myenv下会多出一些文件 进入sc ...

  3. loj2048 「HNOI2016」最小公倍数

    link 题意: 给定一张$N$个顶点$M$条边的无向图(顶点编号为$1,2,...,n$),每条边上带有权值.所有权值都可以分解成$2^a \cdot 3^b$的形式. 现在有$q$个询问,每次询问 ...

  4. python3 开发面试题(面向对象)6.6

    """ 封装.继承.多态 1. 谈谈你对面向对象的理解? 2. Python面向对象中的继承有什么特点? 3. 面向对象深度优先和广度优先是什么? 4. 面向对象中sup ...

  5. 异步接收MSMQ消息

    在这部分,我们将使用ThreadPool 和MSMQ 进行消息收发.MSMQ 是一个分布式队列,通过MSMQ 一个应用程序可以异步地与另外一个应用程序通信. 在一个典型的场景中,我们要向维护一个队列的 ...

  6. [Go] 命令行参数解析包(flag 包)使用详解

    Go 的 flag 包可以解析命令行的参数. 一.命令行语法 命令行语法主要有以下几种形式: cmd -flag       // 只支持bool类型 cmd -flag=xxx cmd -flag ...

  7. delphi Image 处理

    procedure ResizeBmp(Src,Dst:String);var SrcBM,DstBM:TBitMap; Rect:TRect; NewW,NewH,PicW,PicH:Integer ...

  8. JAVA各种系统架构图及其简介

    1.spring架构图 Spring是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为J2EE应用程序开发提供集成的框 ...

  9. Complete uninstall on Mac, HELP!

    Remove these directories: /Applications/Xamarin Studio.app /Developer/MonoTouch /Developer/MonoAndro ...

  10. mybatis学习之路----批量更新数据两种方法效率对比

    原文:https://blog.csdn.net/xu1916659422/article/details/77971696/ 上节探讨了批量新增数据,这节探讨批量更新数据两种写法的效率问题. 实现方 ...