题解 | #最长回文子串#

最长回文子串

https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507

import java.util.Scanner;
public class Main {
    public static boolean fun(String str) {
        char[] ch = str.toCharArray();
        int num = 0;
        boolean flag = false;
        for (int i = 0; i < ch.length; i++) {
            if (ch[i] == ch[ch.length - 1 - i]) {
                num++;
            }
        }
        if (num == str.length()) {
            flag = true;
        }
        return flag;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        int f = 0;
        int max = 0;
        for (int i = 0; i < str.length(); i++) {
            for (int j = 2; j <= str.length() && i + j <= str.length() ; j++) {
                if (fun(str.substring(i, i + j))) {
                    if (str.substring(i, i + j).length() > max) {
                        max = str.substring(i, i + j).length();
                    }
                }
            }
        }
        System.out.println(max);
    }
}

全部评论

相关推荐

哞客37422655...:你猜为什么福利这么好还得一直追着你问
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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