leetcode-Longest Palindromic Substring

来源:互联网 发布:c语言a怎么表示 编辑:程序博客网 时间:2024/06/15 07:34

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) {        string lString = "";        int lnumber = 0;        int tempnumber;        string tempstring;        int n = s.length();        int i;        if(n <= 1)return s;        for(i = 0;i <n-1;i++)        {            int l = i;            int h = i;            tempnumber = 0;            tempstring = "";            while ((l > -1)&&(h < n)&&(s[l] == s[h]))            {                if(l != h)tempnumber += 2;                else tempnumber++;                l--;                h++;            }           if(lnumber < tempnumber)            {                l++;                h--;                tempstring = s.substr(l, h-l+1);                lnumber = tempnumber;                lString = tempstring;            }                        if(s[i] != s[i+1])continue;            l = i;            h = i+1;            tempnumber = 0;            tempstring = "";            while((l > -1)&&(h < n)&&(s[l] == s[h]))            {                tempnumber += 2;                l--;                h++;            }            if(lnumber < tempnumber)            {                l++;                h--;                tempstring = s.substr(l, h-l+1);                lnumber = tempnumber;                lString = tempstring;            }        }        return lString;    }};


0 0
原创粉丝点击