题解 | #最长回文子串#

最长回文子串

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

class HelloWorld {
    public static void main(String[] args) {
      //双层循环 截所有的串 判断串是不是回文串  记录长度取最大
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        int len = s.length();
        int res = 0;
        //System.out.println(isHw("abba"));
        for (int i = 0; i < len; i++) {
            for (int j = i + 1; j <= len; j++) {
                String sb = s.substring(i, j);
                System.out.println(sb);
                if (isHw(sb)) {
                    res = Math.max(res,sb.length());
                }
            }
        }
        System.out.println(res);
    }
      static boolean isHw(String ss) {
        int s = 0, end = ss.length() - 1;
        while (s <= end) {
            if (ss.charAt(s) != ss.charAt(end)) {
                return false;
            }
            s++;
            end--;
        }
        return true;
    }
}
全部评论

相关推荐

头顶尖尖的程序员:我是26届的不太懂,25届不应该是找的正式工作吗?为什么还在找实习?大四还实习的话是为了能转正的的岗位吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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