leetcode------Palindrome Partitioning

标题:

Palindrome Partitioning

通过率: 26.3%
难度: 中等

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

本题还是一个递归的过程,只是再递归的时候要判断是否为回文函数,然后就是字符串的截取,具体看代码

 1 public class Solution {
 2     public ArrayList<ArrayList<String>> partition(String s) {
 3         ArrayList<ArrayList<String>> res=new ArrayList<ArrayList<String>>();
 4         ArrayList<String> tmp=new ArrayList<String>();
 5         getpa(res,tmp,s);
 6         return res;
 7     }
 8     public void getpa(ArrayList<ArrayList<String>> res,ArrayList<String> tmp,String s){
 9         if(s.length()==0||s==null){
10             res.add(new ArrayList<String>(tmp));
11         }
12         else{
13             for(int i=1;i<=s.length();i++){
14                 if(ispa(s.substring(0,i))){
15                     tmp.add(s.substring(0,i));
16                     getpa(res,tmp,s.substring(i));
17                     tmp.remove(tmp.size()-1);
18                 }
19             }
20         }
21     }
22     public boolean ispa(String s){
23         for(int i=0,j=s.length()-1;i<j;i++,j--){
24             if(s.charAt(i)!=s.charAt(j))
25                 return false;
26         }
27         return true;
28     }
29 }
原文地址:https://www.cnblogs.com/pkuYang/p/4341941.html