注意add arraylist<E> 到 ArrayList<ArrayList<E>>的时候传递引用和传值得区别!!!
1 public class Solution { 2 public ArrayList<ArrayList<String>> partition(String s) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>(); 6 if(s == null || s.length() == 0) 7 return result; 8 ArrayList<String> stringList = new ArrayList<String>(); 9 PaPartition(s, 0, s.length(), stringList, result); 10 return result; 11 } 12 private boolean isPalindrome(String s, int start, int end) 13 { 14 while(start < end - 1) 15 { 16 if(s.charAt(start) != s.charAt(end - 1)) 17 return false; 18 start ++; 19 end --; 20 } 21 return true; 22 } 23 private void PaPartition(String s, int start, int end, ArrayList<String> stringList, ArrayList<ArrayList<String>> result) 24 { 25 if(start >= end) 26 { 27 ArrayList<String> tmp = new ArrayList<String>(); 28 tmp.addAll(stringList); 29 result.add(tmp); 30 return; 31 } 32 for(int i = start + 1; i <= end; i++) 33 { 34 if(isPalindrome(s, start, i)) 35 { 36 stringList.add(s.substring(start, i)); 37 PaPartition(s, i, end, stringList, result); 38 stringList.remove(stringList.size() - 1); 39 } 40 } 41 } 42 }