当前位置:网站首页>每日一题-最长回文子串-0714
每日一题-最长回文子串-0714
2022-08-05 05:17:00 【菜鸡程序媛】
题目:
给你一个字符串 s,找到 s 中最长的回文子串。
解题思路:
代码:
class Solution {
public String longestPalindrome(String s) {
if(s == null || s.length() == 0)
return null;
String reverse = new StringBuilder(s).reverse().toString();
int[] arr = new int[s.length()];
int maxLen = 0;
int endX = 0;
for(int i = 0; i < s.length(); i ++){
for(int j = reverse.length() - 1; j >=0; j --){
if(s.charAt(i) == reverse.charAt(j)){
if(j == 0)
arr[j] = 1;
else
arr[j] = arr[j - 1] + 1;
}else
arr[j] = 0;
if(maxLen < arr[j]){
int before = s.length() - 1 - j;
if(before + arr[j] - 1 == i){
maxLen = arr[j];
endX = i;
}
}
}
}
return s.substring(endX - maxLen + 1, endX + 1);
}
}
边栏推荐
猜你喜欢
随机推荐
电子产品量产工具(3)- 文字系统实现
A deep learning code base for Xiaobai, one line of code implements 30+ attention mechanisms.
PID详解
物联网-广域网技术之NB-IoT
【nodejs】第一章:nodejs架构
【UiPath2022+C#】UiPath 练习-数据操作
深度学习系列(二)优化器 (Optimization)
电子产品量产工具(1)- 显示系统实现
【论文阅读-表情捕捉】High-quality Real Time Facial Capture Based on Single Camera
网工必用神器:网络排查工具MTR
LeetCode刷题之第54题
网管日记:故障网络交换机快速替换方法
AIDL detailed explanation
二、自动配置之底层注解
LeetCode刷题之第129题
每日一题-单调栈
ACL 的一点心得
Leetcode刷题——对链表进行插入排序
【UiPath2022+C#】UiPath数据类型
读论文-Cycle GAN