题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param A string字符串
* @return int整型
*/
int condition1(int min_size,string A,int i){
int id=1;
int temp=1;
while(min_size){
if(A[i-id] == A[i+id]){
temp+=2;
id++;
min_size--;
// cout<<"hello"<<endl;
}else{
break;
}
}
return temp;
}
int condition2(int min_size,string A,int i){
int temp=0;
int id=1;
while(min_size){
if(A[i-id+1] == A[i+id]){
temp+=2;
id++;
min_size--;
}else{
break;
}
}
return temp;
}
int getLongestPalindrome(string A) {
// write code here
int rel=1;
for(int i=0;i<A.size()-1;i++){
int min_size;
//以i为中心向两边扩散
if((i-0)<(A.size()-1-i)){
min_size=i-0;
}else{
min_size=A.size()-1-i;
}
int temp1=condition1(min_size, A,i);
//以i为边界比较两边的字符
if((i-0+1)<(A.size()-1-i)){
min_size=i-0+1;
}else{
min_size=A.size()-1-i;
}
int temp2=condition2(min_size, A,i);
if(rel<temp1){
rel=temp1;
}
if(rel<temp2){
rel=temp2;
}
}
return rel;
}
};
联想公司福利 1481人发布