当前位置:网站首页>每日一题-最长回文子串-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);
}
}
边栏推荐
猜你喜欢
CVPR best paper winner Huang Gao's team from Tsinghua University presented the first dynamic network review
网络信息安全运营方法论 (上)
The University of Göttingen proposed CLIPSeg, a model that can perform three segmentation tasks at the same time
CVPR最佳论文得主清华黄高团队提出首篇动态网络综述
读论文-Cycle GAN
PID详解
[Pytorch study notes] 11. Take a subset of the Dataset and shuffle the order of the Dataset (using Subset, random_split)
LeetCode刷题之第61题
CVPR 2022 | 70% memory savings, 2x faster training
七、请求处理——Map、Model类型参数处理原理
随机推荐
九、响应处理——内容协商底层原理
电子产品量产工具(2)- 输入系统实现
Redis设计与实现(第三部分):多机数据库的实现
网工必用神器:网络排查工具MTR
「实用」运维新手一定不能错过的17 个技巧
【论文阅读-表情捕捉】ExpNet: Landmark-Free, Deep, 3D Facial Expressions
链表章6道easy总结(leetcode)
Thread handler handle IntentServvice handlerThread
(C语言)动态内存管理
Service
物联网:LoRa无线通信技术
网络信息安全运营方法论 (上)
HuiFer 带你读懂 BeanFactory getBean 方法
C语言的一些小常识
《基于机器视觉的输电线路交叉点在线测量方法及技术方案》论文笔记
[Kaggle project actual combat record] Steps and ideas sharing of a picture classification project - taking leaf classification as an example (using Pytorch)
电子产品量产工具(4)-UI系统实现
常用 crud 的思考和设计
CVPR2021 - Inception Convolution with Efficient Dilation Search
【ts】typescript高阶:条件类型与infer