Longest Palindromic Substring[leetcode]

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.

class Solution {
public:
    string longestPalindrome(string s) {
        int len = s.length();
        string ans = "";
        for(int i=0;i<len;i++){
            int ind = min(i, len-i-1), j=1;
            for(j=1;j<=ind;j++){
                if(s[i-j] != s[i+j]){
                    break;
                }
            }
            string tmp = s.substr(i-j+1, 2*j-1);
            if(tmp.length() > ans.length())
                ans = tmp;
        }
// assume the length is odd
        for(int i=0;i<len-1;i++){
            if(s[i]==s[i+1]){
                int ind = min(i, len-i-2), j=1;
                for(j=1;j<=ind;j++){
                    if(s[i-j] != s[i+1+j]){
                        break;
                    }
                }
                string tmp = s.substr(i-j+1, 2*j);
                if(tmp.length() > ans.length())
                    ans = tmp;
            }
        }
//assume the length is even
        return ans;
    }
};
时间: 2024-10-28 20:07:42

Longest Palindromic Substring[leetcode]的相关文章

Longest Palindromic Substring leetcode java

题目: 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. 题解: 第一种方法就是挨个检查,维护全局最长,时间复杂度为O(n3),会TLE. 代码如下: 1 public String longestP

Longest Palindromic Substring——LeetCode

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. 题目大意:给一个字符串S,存在唯一的最长的回文子串,求这个回文子串. 解题思路:首先这个题有O(n)的解,是在http://articles.leet

LeetCode #5 Longest Palindromic Substring (M)

[Problem] 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. [Analysis] 这题的思路有很多种,网上也有各种讨论.这里我采用的是个人觉得比较好理解的一种利用Dynamic Progra

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之余全部耗在这上面了,只怪自己基础差.本文主要介绍使用Manacher线性算法来求解字符串的最长回文子字符串. 题目 Given a string S, find the longest palindromic substring in S. You may assume that the maxim

LeetCode 5 Longest Palindromic Substring(最长子序列)

题目来源:https://leetcode.com/problems/longest-palindromic-substring/ 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. 动态规划,类似于l

【LeetCode OJ】Longest Palindromic Substring

题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题目: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. 解题思路:p

[LeetCode][JavaScript]Longest Palindromic Substring

Longest Palindromic Substring 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. https://leetcode.com/problems/longest-palindr

LeetCode(3)题解: Longest Palindromic Substring

https://leetcode.com/problems/longest-palindromic-substring/ 题目: 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. 思路: 我的思路是遍

LeetCode 5:Longest Palindromic Substring(最长回文串)

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. 思路一:(超时)简单的截取长度为length(length递减)的字串,判断其是否为回文串.第一个满足要求的回文串最长. public class S