zoukankan      html  css  js  c++  java
  • [leetcode]question5: Longest Palindromic Substring

    Point: the palindromic should has a center, the center is also the center of the palindromic string.so we can find the longest one.

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

    My solution:

    package com.connie;

    /**
    * Created by lenovo on 02/05/2019.
    */
    public class Leetcode5 {
    public static void main(String[] args) {
    String inputString = "baaabd";
    System.out.print(palindronmic(inputString));
    }
    public static int palindronmic(String inputstring) {
    int maxLength = 1;
    for(int m=0;m<inputstring.length();m++) {
    int gap = 0, n = 0;
    while(m+1+n<inputstring.length() && inputstring.charAt(m+n) == inputstring.charAt(m+1+n)){
    n++;

    }
    while(m-gap>=0 && (m+n+gap)<inputstring.length()&& (inputstring.charAt(m-gap) == inputstring.charAt(m+n+gap))) {
    maxLength = Math.max(maxLength, gap*2+n+1);
    gap++;
    }
    }
    return maxLength;
    }
    }
  • 相关阅读:
    Web学习之css
    Spring学习之第一个hello world程序
    MySQL基础学习总结
    Jmeter参数化
    mysql慢查询解析-linux命令
    mysql慢查询
    mysql_存储引擎层-innodb buffer pool
    mysql_Qcahce
    memocached基础操作
    Memcached安装配置
  • 原文地址:https://www.cnblogs.com/connie313/p/10803866.html
Copyright © 2011-2022 走看看