Middle-题目78：131. Palindrome Partitioning

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

public class Solution {
public List<List<String>> partition(String s) {
List<List<String>> list = new ArrayList<List<String>>();
backtrack(list,new ArrayList<>(),s);
return list;
}
private void backtrack(List<List<String>> list, List<String> sublist, String s) {
if(s.length() == 0) {
}
else {
for(int i = 1; i<=s.length();i++) {
if(isPalindrome(s.substring(0, i))) {
backtrack(list, sublist, s.substring(i));
sublist.remove(sublist.size()-1);
}
}
}
}
public boolean isPalindrome(String s) {
if(s==null||s.length()==0)
return true;

for(int i = 0,j = s.length()-1;i<j;i++,j--) {
if(s.charAt(i)!=s.charAt(j))
return false;
}
return true;
}
}

8ms,beats 31.90%,众数8ms,29.29%

• 本文已收录于以下专栏：

举报原因： 您举报文章：Middle-题目78：131. Palindrome Partitioning 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)