class Go{
List<List<String>> result = new ArrayList<>();
List<String> list = new ArrayList<>();
char[] arr;
String s;
void core(int index){
if (index == arr.length){
result.add(new ArrayList<>(list));
return;
}
for (int i = index; i < arr.length; i++){
if (ishw(index,i)){
list.add(s.substring(index,i + 1));
core(i + 1);
list.remove(list.size() - 1);
}
}
}
boolean ishw(int left,int right){
int L = left,R = right;
while (L <= R){
if (arr[L++] != arr[R--]){
return false;
}
}
return true;
}
public List<List<String>> partition(String s) {
arr = s.toCharArray();
this.s = s;
core(0);
return result;
}
}
public class Cutpalindrome {
public static void main(String[] args) {
Go g = new Go();
System.out.println(g.partition("aacb"));
}
}
Java实现 LeetCode 131 分割回文串
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【达梦系列】分割字符串,返回表之(管道表函数)
文章分类没有达梦数据库,所以只能选择oracle了
字符串 自定义 自定义函数 -
LeetCode 131. 分割回文串(回溯分割)
算法优化:先用dp算出所有子串是否是回文串,就不用频繁调用函数判断当前子串是否是回文串
leetcode 回文串 字符串 子串