LeekCode problem-solving record

There LeekCode last night's game, two and a half hours five topics of the solution, easily resolved after the first two questions, the third card in question, to give up half an hour left to start the fifth problem solution, the solution finished but not verified by the first Look at the four questions about the topic.
Top-ranked heavyweights only 36 minutes all finished.
Differ greatly, only a little bit closer to the big brother.

LeekCode --- 5. Palindromic longest substring
Given a string s, s to find the longest substring palindromic. You can assume that the maximum length of 1000 s.

Problem-solving ideas: Solution of this problem there are several, I talk about ideas (Center Extender) solution of this question of my own. First, three types palindrome: 1: 'baab'; 2: 'bab'; 3: 'a'. Then, it is clear that can be found, from the center of the palindrome are symmetrical (the first two easy to understand, only in the third similar 'abcd' in this field, each character can be seen as a palindrome, it takes the first character as his palindrome). We walk through the fields, each character as a palindrome centers, to compare the first and second case: the length of the first palindrome is even, because traversal, so we only go to compare the current position and his phase system if the previous character, if the same, then the comparison continues before and after the characters are the same; palindromic second length is an odd number, coupled with its need to calculate the length of its length, which in turn compare before and characters are the same. When a plurality of repeated characters occur simultaneously satisfies both the conditions (e.g., 'baaab'), so that each character needs to perform simultaneously two cases parity, and then take a large length.

var longestPalindrome = function(s) {
    if(s.length<2){
        return s;
    }
    let length=0;
    let string='';
    for(let i=1;i<s.length;i++){
        let oddLength=1,evenLength=0;
        let oddStr='',evenStr='';
        for(let j=1;j<=i;j++){
            if(s[i-j]===s[i+j-1]){
                evenLength+=2;
                evenStr=s.substr(i-j,evenLength);
            }else{
                j=i;
            }
        }
        for(let j=1;j<=i;j++){
            if(s[i-j]===s[i+j]){
                oddLength+=2;
                oddStr=s.substr(i-j,oddLength);
            }else{
                j=i;
            }
        }
        string=Math.max(length,evenLength,oddLength)===evenLength?evenStr:Math.max(length,evenLength,oddLength)===oddLength?oddStr:string
        length=Math.max(length,evenLength,oddLength)
    }
    return length===1?s[1]:string;//全为单字符时,取第一个字符
};

This question is a lot of official solution, string negated the way I have considered, because the most time-saving, but how to compare the interception of string I tried many times, and finally gave up. As for brute force, it is to go through all the strings that may exist, and then determine whether it is a palindrome, that I felt foolish, and I think in fact extend from the center in a sense already regarded as a kind of brute force, no need to enumerate all the first string again compared. Dynamic programming algorithm I'm not too familiar, yet do not understand, and so be able to go back and read the supplement it. There is also a Manacher algorithm, it said to be very fast hardware, I want to go look, and then come back up.

Guess you like

Origin www.cnblogs.com/eYan/p/11586383.html
Recommended