Java 最优题解| #最长回文子串#

最长回文子串

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

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        int max = 0;

        for (int i = 0; i < s.length(); i++) 
            for (int j = s.length() - 1; j - i > max; j--) 
                if (isPalindromeString(s, i, j)) max = Math.max(max, j - i + 1);

        System.out.print(max);
    }
    
    static boolean isPalindromeString(String s, int head, int tail) {
        while (head < tail) if (s.charAt(head++) != s.charAt(tail--)) return false;
        return true;
    }
}

双重for剪枝 + 无复制判断

全部评论

相关推荐

找到实习了&nbsp;给了150一天&nbsp;但是说是低代码&nbsp;值得去吗
码农索隆:是在没实习,可去,待个一两周,不行就润呗
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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