/** 131. Palindrome Partitioning * 2015-12-17 by Mingyang * Return all possible palindrome partitioning of S. * 1. Length standard: None (Fixed) * 2. Optional range: From start to last * 3. Go one step further: Temp joins this number, then start plus 1 indicates the next one is added * 4. Step back: Temp minus this number * 5. Special CAs E:start to the last * 6. About Repetition: none * This topic did not add the start parameter at the beginning of the first time, and each time I intercepted the useless string and continued to pass forward * but it is better to use a start index (this will not cut the string Taken to intercept)*/ PublicList<list<string>>partition (String s) {List<String> item =NewArraylist<string>(); List<List<String>> res =NewArraylist<list<string>>(); if(s = =NULL|| S.length () = = 0) returnRes; DFS (S,0, item, RES); returnRes; } Public voidDFS (String s,intStart, list<string> item,list<list<string>>Res) { if(Start = =s.length ()) {Res.add (NewArraylist<string>(item)); return; } for(inti = start; I < s.length (); i++) {String str= S.substring (Start, i + 1);//each round of Dfs comes in first to take the first number, start index, you can not stringbuffer to save if(Ispalindrome (str)) {item.add (str); DFS (s, I+ 1, item, RES);//It takes I, so the next start index is i+1Item.remove (Item.size ()-1); } } } Public Static BooleanIspalindrome (String s) {intLow = 0; intHigh = S.length ()-1; while(Low <High ) { if(S.charat (low)! =S.charat (high))return false; Low++; High--; } return true; }
131. Palindrome Partitioning