题解NC17 | #最长回文子串#

最长回文子串

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

import java.util.*;
import java.util.stream.Collectors;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param A string字符串
     * @return int整型
     */
    public int getLongestPalindrome (String A) {
        int count = 0;
        List<Integer> lengthRecordList = new ArrayList<>();

        boolean[][] check = new boolean[A.length()][A.length()];

        for (int i = A.length() - 1; i >= 0; i--) {
            for (int j = i; j < A.length(); j++) {
                if (A.charAt(i) == A.charAt(j)) {
                    if (j - i < 2) {
                        check[i][j] = true;
                        count++;
                        lengthRecordList.add(j - i + 1);
                    } else if (check[i + 1][j - 1] == true) {
                        check[i][j] = true;
                        count++;
                        lengthRecordList.add(j - i + 1);
                    }
                }
            }
        }

        return  lengthRecordList.stream().sorted(
                    Comparator.reverseOrder()).collect(Collectors.toList()).get(0);
    }
}

一天就做了一题 明天一定要把剩下的做完

全部评论

相关推荐

头像
不愿透露姓名的神秘牛友
05-27 11:41
已编辑
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务