题解 | #所有的回文子串II#
所有的回文子串II
https://www.nowcoder.com/practice/3373d8924d0e441987650194347d3c53
题目考察的知识点
考察回文串
题目解答方法的文字分析
遍历枚举所有的组合,对这些字符串进行回文判断即可
本题解析所用的编程语言
使用Java代码解答
完整且正确的编程代码
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return string字符串一维数组
*/
public String[] partitionII (String s) {
// write code here
TreeSet<String> set = new TreeSet<>((o1, o2) -> o1.compareTo(o2));
for (int i = 0; i < s.length(); i++) {
for (int j = i + 1; j < s.length(); j++) {
if (check(s, i, j)) {
set.add(s.substring(i, j + 1));
}
}
}
String[] res = new String[set.size()];
set.toArray(res);
return res;
}
private boolean check(String s, int i, int j) {
while (i < j) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
}