题解 | #最长回文子串#

最长回文子串

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;
    }
}
全部评论

相关推荐

06-26 22:20
门头沟学院 Java
码农索隆:让你把简历发给她,她说一些套话,然后让你加一个人,说这个人给你改简历,然后开始卖课
我的求职精神状态
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务