zoukankan      html  css  js  c++  java
  • 5. Longest Palindromic Substring(C++)

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

    Example:

    Input: "babad"
    
    Output: "bab"
    
    Note: "aba" is also a valid answer.
    

    Example:

    Input: "cbbd"
    
    Output: "bb"

    答案:

    class Solution {
    public:
      string longestPalindrome(string s) {
        if(s.size()<2){
          return s;
        }
        int len = s.size(), maxlen = 1, maxleft = 0, left, right;
        for(int i=0;i<len&&len-i > maxlen/2;){
          left = right = i;
          while(right<len-1 && s[right]==s[right+1]){
            right++;
          }
          i=right+1;
          while(right<len-1 && left>0 && s[left-1]==s[right+1]){
            right++;
            left--;
          }
          if(maxlen<right-left+1){
            maxleft = left;
            maxlen =right-left+1;
          }
        }
        return s.substr(maxleft,maxlen);
      }
    };

  • 相关阅读:
    读入输出优化
    【JSOI2008】星球大战 并查集
    堆STL和重载运算符
    树的直径
    H3C三层交换机(S5500)清除配置信息并进行简单配置
    简单的逻辑学
    Java基础--第十八天
    Java基础--第十七天
    Java基础--第十六天
    Java基础--第十五天
  • 原文地址:https://www.cnblogs.com/devin-guwz/p/6480252.html
Copyright © 2011-2022 走看看