题解 | #最长回文子串#

最长回文子串

https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507

#include <iostream>
#include <string>
using namespace std;

bool isPalindromeString(const string& str, int pos, int len) {
    int s = pos, e = pos + len - 1;
    while (s <= e) {
        if (str[s] == str[e]) {
            s++;
            e--;
            continue;
        } else {
            return false;
        }
    }
    return true;
}

int main() {
    string str;
    getline(cin, str);
    for (int len = str.length(); len >= 1; len--) {
        for (int i = 0; i <= str.length()-len; i++) {
            if (isPalindromeString(str, i, len)) {
                cout << len << endl;
                return 0;
            }
        }
    }
    cout << 0 << endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务