当前位置:网站首页>每日一题-最长回文子串-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);
}
}
边栏推荐
猜你喜欢

七、请求处理——Map、Model类型参数处理原理

(oj)原地移除数组中所有的元素val、删除排序数组中的重复项、合并两个有序数组

五、请求处理—Rest映射是怎样实现的?

电子产品量产工具(3)- 文字系统实现

Tensorflow steps on the pit notes and records various errors and solutions

The University of Göttingen proposed CLIPSeg, a model that can perform three segmentation tasks at the same time

MaskDistill - Semantic segmentation without labeled data

You should write like this

1008 数组元素循环右移问题 (20 分)

【UiPath2022+C#】UiPath 数据操作
随机推荐
网络信息安全运营方法论 (中)
【UiPath2022+C#】UiPath 数据操作
1004 成绩排名 (20 分)
伪RTOS-ProroThread在CH573芯片上的移植
C语言入门笔记 —— 分支与循环
物联网-广域网技术之NB-IoT
多边形等分
SQL(1) - Add, delete, modify and search
网工必用神器:网络排查工具MTR
CAN、CAN FD
九、响应处理——内容协商底层原理
每日一题-字典
MySQL
【ts】typescript高阶:分布式条件类型
LeetCode刷题之第86题
对象比较
吞吐?带宽?傻傻分不清楚
OSPF网络类型
电子产品量产工具(5)- 页面系统实现
[Kaggle project actual combat record] Steps and ideas sharing of a picture classification project - taking leaf classification as an example (using Pytorch)