leet-code/src/main/java/leetcode/editor/cn/LongestPalindromicSubstring.java
huangge1199@hotmail.com f4b049d5c8 5:最长回文子串
2021-04-30 00:15:43 +08:00

104 lines
2.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给你一个字符串 s找到 s 中最长的回文子串。
//
//
//
// 示例 1
//
//
//输入s = "babad"
//输出:"bab"
//解释:"aba" 同样是符合题意的答案。
//
//
// 示例 2
//
//
//输入s = "cbbd"
//输出:"bb"
//
//
// 示例 3
//
//
//输入s = "a"
//输出:"a"
//
//
// 示例 4
//
//
//输入s = "ac"
//输出:"a"
//
//
//
//
// 提示:
//
//
// 1 <= s.length <= 1000
// s 仅由数字和英文字母(大写和/或小写)组成
//
// Related Topics 字符串 动态规划
// 👍 3593 👎 0
package leetcode.editor.cn;
import java.util.Collections;
//5:最长回文子串
public class LongestPalindromicSubstring {
public static void main(String[] args) {
//测试代码
Solution solution = new LongestPalindromicSubstring().new Solution();
//bab
System.out.println(solution.longestPalindrome("babad"));
//bb
System.out.println(solution.longestPalindrome("cbbd"));
//a
System.out.println(solution.longestPalindrome("a"));
//a
System.out.println(solution.longestPalindrome("ac"));
//ccc
System.out.println(solution.longestPalindrome("ccc"));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String longestPalindrome(String s) {
int length = s.length();
if (length < 2) {
return s;
}
boolean[][] bl = new boolean[length][length];
for (int i = 0; i < length; i++) {
bl[i][i] = true;
}
int index = 0;
int max = 1;
for (int i = 2; i <= length; i++) {
for (int start = 0; start < length && i + start - 1 < length; start++) {
int end = i + start - 1;
if (s.charAt(start) != s.charAt(end)) {
bl[start][end] = false;
} else {
if (i <= 3) {
bl[start][end] = true;
} else {
bl[start][end] = bl[start + 1][end - 1];
}
}
if (bl[start][end] && i > max) {
max = i;
index = start;
}
}
}
return s.substring(index, index + max);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}