题解 | #在字符串中找出连续最长的数字串#

在字符串中找出连续最长的数字串

https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec

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

int main() {
    string str;
    char c;
    string temp;
    string max;
    int maxLen = 0;
    getline(cin, str);

    for (char c : str) {
        if (isdigit(c)) {
            temp += c;
        } else {
            if (!temp.empty()) {
                if (temp.length() > maxLen) { //temp长度大于max就赋值
                    max = temp;
                    maxLen = max.length();
                } else if (temp.length() == maxLen) {
                    max += temp;
                }
                // maxLen = max.length();
                // max = temp;
            }
            temp = "";
        }
    }

    // max = maxLen > temp.length() ? max : temp;

    if (temp.length() > maxLen) { //temp长度大于max就赋值
        max = temp;
        maxLen = temp.length();
    } else if (temp.length() == max.length()) {
        max += temp;
    }

    cout << max << "," << maxLen << endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务