题解 | #最长回文子串#

最长回文子串

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

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        
        String line=sc.nextLine();
        String s;
        int max=0;
        for(int i=0;i<line.length();i++){
            for(int j=line.length();j>i;j--){
                s=line.substring(i,j);
                if(line.length()-i<max) break;
                if(check(s)&&s.length()>max) max=s.length();
            }
        }
        System.out.println(max);
    }
    
    public static boolean check(String s){
        return s.equals(new StringBuilder(s).reverse().toString());
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-23 14:22
点赞 评论 收藏
分享
水墨不写bug:疑似没有上过大学
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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