标签:
正常dfs
public class Solution { public ArrayList<ArrayList<String>> partition(String s) { ArrayList<ArrayList<String>> res = new ArrayList<ArrayList<String>>(); if(s==null||s.length()==0) return res; ArrayList<String> item = new ArrayList<String>(); dfs(s, 0, item, res); return res; } private 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 tmp = s.substring(start,i+1); if(isPalindrome(tmp)){ item.add(tmp); dfs(s,i+1,item,res); item.remove(item.size()-1); } } } private boolean isPalindrome(String t){ if(t==null||t.length()<2) return true; int low=0, high=t.length()-1; while(low<high){ if(t.charAt(low)!=t.charAt(high)) return false; low++; high--; } return true; } }
标签:
原文地址:http://www.cnblogs.com/jiajiaxingxing/p/4568568.html