题解 | #最长回文子串#

最长回文子串

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

1.for循环嵌套截取子串;
2.判断子串是否是回文字符串;
3.计算个子串中长度的最大值;
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            String str = sc.nextLine();
            //StringBuilder sb = new StringBuilder();
            //sb.append(str);
            //sb.reverse();
            int max = 0;
            for(int i = 0 ;i < str.length(); i++){
                for(int j = i + 1; j <= str.length(); j++){
                    StringBuilder sb = new StringBuilder();
                    sb.append(str.substring(i,j));
                    sb.reverse();
                    if(str.substring(i,j).contentEquals(sb)){
                        if(j - i > max){
                            max = j - i;
                        }
                    }
                }
            }
            System.out.println(max);
        }
    }
}


全部评论

相关推荐

挥毫自在:想白嫖你呢
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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