题解 | #最长回文子串#

最长回文子串

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

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            String str = in.next();
            int max = 1;
            for(int i=0; i<str.length(); i++){
                for(int j=str.length(); j>i; j--){
                    String subStr = str.substring(i, j);
                    if(isHW(subStr)){
                        max = Math.max(max, j-i);
                        break;
                    }
                }
            }
            System.out.println(max);
        }
    }
    

    
    public static boolean isHW(String str){
        int i = 0;
        int j = str.length()-1;
        while(i<=j){
            if(str.charAt(i++) != str.charAt(j--)){
                return false;
            }
        }
        return true;
    }
}
全部评论

相关推荐

不对是145个人…嗯…&nbsp;大家都没发现秋招提前批来了嘛..笑死我了
牛客39712426...:投了也是浪费时间,之前投米实习,除了浪费我时间写笔试题没有任何反馈,懒得投了
26届校招投递进展
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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