当前位置:网站首页>Leetcode 5. Longest Palindromic Substring
Leetcode 5. Longest Palindromic Substring
2022-06-27 16:58:00 【chenyson】
difficulty : secondary
frequency :117
subject : Give you a string s, find s The longest palindrome substring in .
How to solve the problem : Center expansion method
- 1. If string It's odd , So it is a single center expansion .
- 2. If it's an even number , So it's a two center expansion .
- 3. The return value is the length of palindrome substring
- 4. According to length and Center , Find substring
class Solution {
public String longestPalindrome(String s) {
int len=s.length();
if(len<=1) return s;
int [] res=new int[2];
int maxlen=0;
//for Loop cannot be i and i+1 Transboundary .
for(int i=0;i<len-1;i++){
int [] odd=centerspread(s,i,i);
int [] even=centerspread(s,i,i+1);
int [] max=odd[1]>even[1]?odd:even;
if(max[1]>maxlen){
res=max;
maxlen=max[1];
}
}
return s.substring(res[0],res[0]+res[1]);
}
// The return value of the function is An array { first place : Start subscript , Second : length }
public int[] centerspread(String s,int left,int right){
int len=s.length();
while(left>=0&&right<=len-1){
// The methods here need to be remembered
if(s.charAt(left)==s.charAt(right)){
right++;
left--;
}
else{
break;
}
}
return new int[]{
left+1,(right-1)-(left+1)+1}; // there left and right One more
}
}
Be careful :
- Write one more function to calculate the maximum number of palindromes after fixing the center each time
- Remember here s.charAt( Subscript )
- The return value of the function is An array { first place : Start subscript , Second : length }
- final right and left It's an extra step
- Because I don't know whether the maximum palindrome is an odd length or an even length , So it all counts , Then take the longest . From the center 0 To i-2 It's all traversal .
- for In circulation i and i+1 Don't cross the border
边栏推荐
- Oracle concept 3
- IDE Eval reset unlimited trial reset
- Handling of difficult and miscellaneous problems during the installation and configuration of qt5.5.1 desktop version (configuring arm compilation Kit)
- Delete duplicate elements in the sorting linked list
- Realize simple three-D cube automatic rotation
- 等保2.0密码要求是什么?法律依据有哪些?
- A large number of missing anchor text
- Popularization of MCU IO port: detailed explanation of push-pull output and open drain output
- d3dx9_ Where is 35.dll? d3dx9_ Where can I download 35.dll
- 事务的隔离级别详解
猜你喜欢
# Cesium实现卫星在轨绕行
【多线程】线程通信调度、等待集 wait() 、notify()
About MySQL: the phenomenon and background of the problem
P.A.R.A 方法在思源的简易应用(亲测好用)
模拟进程调度
10分钟掌握mysql的安装步骤
数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【LeetCodeHot100】
Open source 23 things shardingsphere and database mesh have to say
EMQ helps Qingdao Yanbo build a smart water platform
Sliding window + monotone queue concept and example (p1886 Logu)
随机推荐
d3dx9_ How to repair 38.dll? d3dx9_ 38. How to download a missing DLL?
等保2.0密码要求是什么?法律依据有哪些?
如何提升IT电子设备效能管理
Some details of Huawei OSPF
07. Express routing
Data center table reports realize customized statistics, overtime leave summary record sharing
ORM表关系及操作
Sliding window + monotone queue concept and example (p1886 Logu)
【多线程】线程通信调度、等待集 wait() 、notify()
Deeply digitise, lead cloud nativity and serve more developers
Etcd visualization tool: kstone deployment (I), rapid deployment based on Helm
#yyds干货盘点# 解决剑指offer:二叉树中和为某一值的路径(三)
10 minutes to master the installation steps of MySQL
d3dx9_ 39.dll how to repair -d3dx9_ 39.dll missing file download
Detailed explanation of transaction isolation level
华为云首次解读云原生2.0十大典型架构,加速构建现代化应用
关于#mysql#的问题:问题遇到的现象和发生背景
What are the password requirements for waiting insurance 2.0? What are the legal bases?
[multithreading] thread communication scheduling, waiting set wait(), notify()
Oracle concept 3