zoukankan      html  css  js  c++  java
  • leetCode刷题(找到最长的回文字符串)

    Given a string, find the length of the longest substring without repeating characters.

    Examples:

    Given "abcabcbb", the answer is "abc", which the length is 3.

    Given "bbbbb", the answer is "b", with the length of 1.

    Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequenceand not a substring.

    /**
     * @param {string} s
     * @return {string}
     */
    var longestPalindrome = function(s) {
      var maxLength="";
      var maxPalindrome="";
      for(index=0;index<s.length;index++){
          var strOdd=getPalindromeLength(index,index,s);
          var strEven=getPalindromeLength(index,index+1,s);
          var strOddLength=strOdd.length;
          var strEvenLength=strEven.length;
          if(maxLength<strOddLength){
              maxLength=strOddLength;
              maxPalindrome=strOdd;
          }
          if(maxLength<strEvenLength){
              maxLength=strEvenLength;
              maxPalindrome=strEven;
          }
      }
      function getPalindromeLength(start,end,Palindrome){
          while((start>=0)&&(end<Palindrome.length)&&(Palindrome[start]==Palindrome[end])){
                start--;
                end++;
          }
          return Palindrome.substring(start+1,end);
      }
      return maxPalindrome
    };
    

      

      

    学而不思则罔,思而不结则殆,结而不看,一事无成
  • 相关阅读:
    Noip2012 开车旅行
    「NOI2018」归程
    2019.10.30 队测(晚上)
    洛谷P1138 第k小整数
    洛谷P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold
    Noip-pj2018游记
    洛谷P4994 终于结束的起点
    《退役的你》
    《膜你抄》
    洛谷P5087 数学
  • 原文地址:https://www.cnblogs.com/windseek/p/8657977.html
Copyright © 2011-2022 走看看