码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode------Palindrome Partitioning

时间:2015-03-16 16:07:04      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

标题:

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 }

 

leetcode------Palindrome Partitioning

标签:

原文地址:http://www.cnblogs.com/pkuYang/p/4341941.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!