int check(int front,int rear,int size,char* s){
if( front-rear+1 <= size ) return 0;
while ( rear<=front ){
if( s[rear] != s[front] )
break;
rear++;
front--;
}
if(rear >front) return 1;
return 0;
}
char* longestPalindrome(char* s) {
int front,rear;
char *p;
int len=strlen(s);
int size=0;
p=(char*)malloc(1001*sizeof(char));
if(len==0||len==1) return s;
rear=0;
front=len-1;
while(rear<=len-1){
front=len-1;
if( front-rear+1 <= size ) break;
while(rear<=front){
if( front-rear+1 <= size ) break;
if(s[rear] == s[front]){
if( check(front,rear,size,s) ) {
strcpy(p,&s[rear]);
p[front-rear+1]='\0';
front--;
size=front-rear+1;
break;
}
}
front--;
}
rear++;
}
return p;
}
从零开始的leetcode刷题——第5题
最新推荐文章于 2020-07-06 07:39:45 发布