NC141 判断回文
记录:


下边这个也行:

bool judge(char* str ) {
// write code here
// write code here
char *pe=str;
char *pa=str;
int t=0;
while( *pe != ‘\0’)
{
t++;
pe++;
}
pe–;
if(t==1)
return true;
while(pa<pe)
{
if(*pa!=*pe)
return false;
pa++;
pe–;
}
return true;
}
NC17 最长回文子串


import java.util.*;
public class Solution {
public int getLongestPalindrome(String A, int n) {
// write code here
//边界条件判断
if (n < 2)
return A.length();
//maxLen表示最长回文串的长度
int maxLen = 0;
for (int i = 0; i < n; ) {
//如果剩余子串长度小于目前查找到的最长回文子串的长度,直接终止循环
// (因为即使他是回文子串,也不是最长的,所以直接终止循环,不再判断)
if (n - i <= maxLen / 2)
break;
int left = i;
int right = i;
while (right < n - 1 && A.charAt(right + 1) == A.charAt(right))
++right; //过滤掉重复的
//下次在判断的时候从重复的下一个字符开始判断
i = right + 1;
//然后往两边判断,找出回文子串的长度
while (right < n - 1 && left > 0 && A.charAt(right + 1) == A.charAt(left - 1)) {
++right;
--left;
}
//保留最长的
if (right - left + 1 > maxLen) {
maxLen = right - left + 1;
}
}
//截取回文子串
return maxLen;
}
}
参考链接:
原文写的特别好,几种解法,有讲解
1029

被折叠的 条评论
为什么被折叠?



