【题目描述】
给你一个字符串 s
,找到 s
中最长的回文子串。
https://leetcode.cn/problems/longest-palindromic-substring/?favorite=2cktkvj
【示例】
【代码】BaoLi
import java.awt.image.ImageProducer;
import java.util.*;
import java.util.stream.Collectors;
// 2022-12-21
class Solution {
public String longestPalindrome(String str) {
int len = str.length();
if (len < 2) return str; // 至少2个字符
int maxLen = 1;
int begin = 0;
char[] chars = str.toCharArray();
for (int i = 0; i < len - 1; i++){
for (int j = i + 1; j < len; j++){
if (j - i + 1 > maxLen && validPalindromic(chars, i, j)){
maxLen = j - i + 1;
begin = i;
}
}
}
System.out.println(str.substring(begin, begin + maxLen));
return str.substring(begin, begin + maxLen);
}
private boolean validPalindromic(char[] chars, int left, int right) {
while (left < right){
if (chars[left] != chars[right]){
return false;
}
left++;
right--;
}
return true;
}
}
public class Main{
public static void main(String[] args) {
String str = "babad";
new Solution().longestPalindrome(str); // 输出 bab
new Solution().longestPalindrome("aaaa"); // 输出 aaaa
}
}
待后序继续研究一下
官方代码
【代码】推荐
方案1: 简单 但超时 2个for循环来轮流获取字符 然后递归判断是否回文
import java.util.*;
// 2022-12-16
class Solution {
public String longestPalindrome(String s) {
String res = "";
for (int i = 0; i < s.length(); i++){
for (int j = i; j < s.length(); j++){
String substring = s.substring(i, j + 1);
if (substring.length() > res.length() && recur(substring)){
res = substring;
}
}
}
System.out.println(res);
return res;
}
// 判断回文
private boolean recur(String s) {
// 边界判断是否是回文
if (s.length() == 0 || s.length() == 1){
return true;
}
// 判断第一个字符是否和最后一个字符相同
return s.charAt(0) == s.charAt(s.length() - 1) && recur(s.substring(1, s.length() - 1));
}
}
public class Main{
public static void main(String[] args) {
String str = "babad";
new Solution().longestPalindrome(str);
}
}
【代码】动态规划
如果 S[i+1,j−1]S[i+1,j-1]S[i+1,j−1] 是回文串,那么只要 S[i]S[ i ]S[i] == S[j]S[ j ]S[j],就可以确定 S[i,j]S[i,j]S[i,j]也是回文串了
package com.company;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
String str = "babad";
longestPalindrome(str);
}
public static String longestPalindrome(String s) {
int begin = 0;
int end = 0;
// 默认都是 false
boolean[][] cache = new boolean[s.length()][s.length()];
for(int n = 0; n < s.length(); n++){
// i和j的差距固定为 N
for (int i = 0, j = i + n; j < s.length(); i++, j++) {
boolean flag ; // 用于记录 i 到 j 范围的子串 是否为回文串
if( i == j){
// 子串长度为1
flag = true;
}else if(j - i == 1){
// 子串长度为2
flag = s.charAt(i) == s.charAt(j);
}else {
// 子串长度大于2
// 如果2个字符相等且去除首尾后也字符相等则是回文字符,
// 字符i == 字符j 且 cache[i+1][j-1]
flag = s.charAt(i) == s.charAt(j) && cache[i+1][j-1];
}
cache[i][j] = flag;
if(flag && (j - i > end - begin)){
begin = i;
end = j;
}
}
}
System.out.println(s.substring(begin, end + 1));
return s.substring(begin, end + 1);
}
}
【代码】admin
这里的代码属于【暴力POJIE】, 部分代码可以执行成功,133/143 这里仅保留
package com.company;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class threeSum {
public static void main(String[] args) {
String str = "babad";
String str1 = "a";
// String str2 = "cbbd";
String str2 = "aacabdkacaa";
String str3 = "bb";
String str4 = "ccc"; // ccc
// longestPalindrome(str);
// longestPalindrome(str1);
System.out.println(longestPalindrome(str2));
// longestPalindrome(str3);
// longestPalindrome(str4);
}
public static String longestPalindrome(String s) {
String result = null;
List<String> list = new ArrayList<>();
if(huiwen(s)){
result = s;
return result;
}
int max = Integer.MIN_VALUE;
for(int i = 0; i < s.length(); i++){
for(int j = i + 2; j <= s.length(); j++){
String ss = s.substring(i, j);
if(huiwen(ss)){
if(ss.length() >= max){
max = ss.length();
list.add(ss);
}
}
}
}
if(list.size() > 0){
result = list.get(list.size() - 1);
}else{
result = s.charAt(0)+"";
}
return result;
}
private static boolean huiwen(String s) {
boolean flag = false;
String x = new StringBuilder(s).reverse().toString();
if(x.equals(s)){
flag = true;
}
return flag;
}
}