solution.md 4.5 KB
Newer Older
1
# 最长回文子串
F
fix bug  
feilong 已提交
2

3
<p>给你一个字符串 <code>s</code>,找到 <code>s</code> 中最长的回文子串。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "babad"<strong><br />输出:</strong>"bab"<strong><br />解释:</strong>"aba" 同样是符合题意的答案。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "cbbd"<strong><br />输出:</strong>"bb"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "a"<strong><br />输出:</strong>"a"</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = "ac"<strong><br />输出:</strong>"a"</pre><p> </p><p><strong>提示:</strong></p><ul>	<li><code>1 <= s.length <= 1000</code></li>	<li><code>s</code> 仅由数字和英文字母(大写和/或小写)组成</li></ul>
每日一练社区's avatar
每日一练社区 已提交
4
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
5

6
## aop
F
fix bug  
feilong 已提交
7

8
### before
F
fix bug  
feilong 已提交
9

10 11 12 13
```cpp
#include <bits/stdc++.h>
using namespace std;
```
每日一练社区's avatar
每日一练社区 已提交
14

15
### after
F
fix bug  
feilong 已提交
16

17 18 19 20 21 22 23 24 25 26 27 28 29 30
```cpp
int main()
{
    Solution test;

    string ret;
    string s = "cbbd";
    ret = test.longestPalindrome(s);
    cout << ret << endl;
    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
```cpp
class Solution
{
public:
    string longestPalindrome(string s)
    {
        int len = s.size();
        if (len == 0 || len == 1)
            return s;
        int start = 0;
        int end = 0;
        int mlen = 0;
        for (int i = 0; i < len; i++)
        {
            int len1 = expendaroundcenter(s, i, i);
            int len2 = expendaroundcenter(s, i, i + 1);
            mlen = max(min(len1, len2), mlen);
            if (mlen > end - start + 1)
            {
                start = i - (mlen - 1) / 2;
                end = i + mlen / 2;
            }
        }
        return s.substr(start, mlen);
    }

private:
    int expendaroundcenter(string s, int left, int right)

    {
        int L = left;
        int R = right;
        while (L >= 0 && R < s.length() && s[R] == s[L])
        {
            L--;
            R++;
        }
        return R - L - 1;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
75

76
### A
F
fix bug  
feilong 已提交
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
```cpp
class Solution
{
public:
    string longestPalindrome(string s)
    {
        int len = s.size();
        if (len == 0 || len == 1)
            return s;
        int start = 0;
        int max = 1;
        vector<vector<int>> dp(len, vector<int>(len));
        for (int i = 0; i < len; i++)
        {
            dp[i][i] = 1;
            if (i < len - 1 && s[i] == s[i + 1])
            {
                dp[i][i + 1] = 1;
                max = 2;
                start = i;
            }
        }
        for (int l = 3; l <= len; l++)
        {
            for (int i = 0; i + l - 1 < len; i++)
            {
                int j = l + i - 1;
                if (s[i] == s[j] && dp[i + 1][j - 1] == 1)
                {
                    dp[i][j] = 1;
                    start = i;
                    max = l;
                }
            }
        }
        return s.substr(start, max);
    }
};
```

### B
F
fix bug  
feilong 已提交
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
```cpp
class Solution
{
public:
    string longestPalindrome(string s)
    {
        if (s.length() == 1)
            return s;
        string rev = s;
        string res;
        std::reverse(rev.begin(), rev.end());
        if (rev == s)
            return s;
        int len = 0;
        for (int i = 0; i < s.length(); i++)
        {
            string temp;
            for (int j = i; j < s.length(); j++)
            {
                temp = temp + s[j];
                if (len >= temp.length())
                    continue;
                else if (rev.find(temp) != -1)
                {
                    string q = temp;
                    std::reverse(q.begin(), q.end());
                    if (q == temp)
                    {
                        len = temp.length();
                        res = temp;
                    }
                }
                else
                    break;
            }
            temp = "";
        }
        return res;
    }
};
```

### C
F
fix bug  
feilong 已提交
163

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
```cpp
class Solution
{
public:
    string longestPalindrome(string s)
    {
        string res = "";
        string temp = "";
        for (int i = 0; i < s.length(); i++)
        {
            for (int j = i; j < s.length(); j++)
            {
                temp = temp + s[j];
                string tem = temp;
                std::reverse(tem.begin(), tem.end());
                if (temp == tem)
                    res = res.length() > temp.length() ? res : temp;
            }
            temp = "";
        }
        return res;
    }
};
```