题解 | #最长回文子串#

最长回文子串

http://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af

import java.util.*;

public class Solution {
    public int getLongestPalindrome(String A, int n) {
        // write code here
        char[] cc = A.toCharArray();
        int res = 0;
        for(int i = 0; i < cc.length; ++i){
            int L = i, R = i, RR = i;
            // 如果相同,L,R指标就跳过去
            while(L - 1 >= 0 && cc[L - 1] == cc[i]) L--;
            while(R + 1 <= 0 && cc[R + 1] == cc[i]) R++;
            RR = R;
            while(L - 1 >= 0 && R + 1 <= cc.length - 1 && cc[L - 1] == cc[R + 1]){
                L--;
                R++;
            }
            res = Math.max(res, R - L + 1);
            i = RR;
        }
        return res;
    }
}
全部评论

相关推荐

28小凳也想实习:项目不用一个业务一个轮子吗,刷牛客好多人说要一业务一轮子
点赞 评论 收藏
分享
02-10 21:39
Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务