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

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

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

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

int main() {
    string str;

    while (cin >> str) {

        string num_str = "";
        vector<string> str_vec;
        for (int i = 0; i <= str.length(); ++i) {

            if (i < str.length()) {
                auto item = str[i];
                if (isdigit(item)) {
                    num_str += item;
                    continue;
                }
            }

            if (num_str.empty()) continue;
            if (str_vec.empty()) {
                str_vec.push_back(num_str);
                num_str = "";
                continue;
            }

            if (num_str.length() == str_vec[0].length()) {
                str_vec.push_back(num_str);
            }
            if (num_str.length() > str_vec[0].length()) {
                str_vec.clear();
                str_vec.push_back(num_str);
            }
            num_str = "";

        }

        for (auto item : str_vec) cout << item;
        cout << "," << str_vec[0].length() << endl;

    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务