Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

最长回文子串 #181

Open
TieMuZhen opened this issue Apr 23, 2022 · 0 comments
Open

最长回文子串 #181

TieMuZhen opened this issue Apr 23, 2022 · 0 comments
Labels

Comments

@TieMuZhen
Copy link
Owner

image

/**
 * @param {string} s
 * @return {string}
 */
var longestPalindrome = function(s) {
    let res = "";
    for(let i = 0; i < s.length; i++){
        // 当i为奇数的中心时
        let left = i - 1, right = i + 1;
        while(left >= 0 && right < s.length && s[left] == s[right]){
            left--;
            right++;
        }
        if(res.length < right - left - 1){
            res = s.substr(left + 1, right - left - 1);
        }
        // 当i为偶数的中心时
        left = i, right = i + 1;
        while(left >= 0 && right < s.length && s[left] == s[right]){
            left--;
            right++;
        }
        if(res.length < right - left - 1){
            res = s.substr(left + 1, right - left - 1);
        }
    }
    return res;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant