[Leetcode学习-c++&java]Longest Palindromic Substring

problem:

Difficulty: medium

Description:

Given a string, find the longest palindrome substring, which is a continuous subset of the string set.

Subject link: https://leetcode.com/problems/longest-palindromic-substring/

Input range:

  • 1 <= s.length <= 1000
  • s consist of only digits and English letters (lower-case and/or upper-case),

Enter the case:

Example 1:
Input: s = "babad"
Output: "bab"
Note: "aba" is also a valid answer.

Example 2:
Input: s = "cbbd"
Output: "bb"

Example 3:
Input: s = "a"
Output: "a"

Example 4:
Input: s = "ac"
Output: "a"

My code:

Fortunately, the input length is less than 1000, so don't be afraid of violent death.

Generally, there are two situations:

1. The palindrome string sandwiched between odd bab is a letter a

2. Even baab sandwiched between two a

Then it is to traverse the array, and judge the odd and even palindrome at each step, but there is a better way to directly eliminate the subsequent repeated characters at each step.

str = "baab" Suppose I go to str[1] and I will exclude duplicates. If it is baab, I will exclude str[2] and make a palindrome judgment. This way, there will be one less comparison. If it is an odd bab, It will not be affected, so the more repeated characters, the higher the efficiency.

Java:

class Solution {
    public String longestPalindrome(String s) {
        char[] chs = s.toCharArray();
        int len = chs.length, cur = 0, max = 0, left = 0;
        while(cur < len) {
            int tcur = cur, tpre = cur;
            while(tcur + 1 < len && chs[tcur] == chs[tcur + 1]) tcur ++; // 排除掉重复
            cur = tcur == cur ? cur + 1 : tcur + 1;
            while(tpre >= 0 && tcur < len && chs[tcur] == chs[tpre]) {// 再进行回文比较
                tcur ++; tpre --;
            }
            int temp = tcur - tpre - 1;
            if(temp > max) { // 获取最长
                max = temp; left = tpre + 1;
            }
        }
        return s.substring(left, left + max); // 省了个 right
    }
}

C++:

class Solution {
public:
    string longestPalindrome(string s) {
        int len = s.length(), max = 0, cur = 0, left = 0;
        while(cur < len) {
            int tcur = cur, tpre = cur;
            while(tcur + 1 < len && s[tcur] == s[tcur + 1]) tcur ++;
            cur = tcur == cur ? cur + 1 : tcur + 1;
            while(tpre >= 0 && tcur < len && s[tpre] == s[tcur]) {
                tpre --; tcur ++;
            }
            int temp = tcur - tpre - 1;
            if(temp > max) {
                max = temp; left = tpre + 1;
            }
        }
        return s.substr(left, max); // c++是从 i 开始,剪多少个字符
    }
};

If you don’t rule out duplication and use odd and even numbers, this is the case

Java:

    public String longestPalindrome(String s) {
        char[] chs = s.toCharArray();
        int len = chs.length, cur = 1, max = 0, maxLeft = 0, maxRight = 0;
        for(;cur < len;cur ++) {
            if(chs[cur - 1] == chs[cur]) { // 偶数
                int tpre = cur - 1, tcur = cur;
                while(tpre >= 0 && tcur < len && chs[tpre] == chs[tcur]) {
                    tpre --; tcur ++;
                }
                int temp = tcur - tpre - 1;
                if(temp > max) {
                    maxLeft = tpre + 1; maxRight = tcur - 1; max = temp;
                }
            }
            if(cur + 1 < len && chs[cur + 1] == chs[cur - 1]) { // 奇数
                int tpre = cur - 1, tcur = cur + 1;
                while(tpre >= 0 && tcur < len && chs[tpre] == chs[tcur]) {
                    tpre --; tcur ++;
                }
                int temp = tcur - tpre - 1;
                if(temp > max) {
                    maxLeft = tpre + 1; maxRight = tcur - 1; max = temp;
                }
            }
        }
        return s.substring(maxLeft, maxRight + 1);
    }

 

Guess you like

Origin blog.csdn.net/qq_28033719/article/details/112858976