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

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

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

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
    string str;
    getline(cin, str);
    string s1 = "";
    vector<string> s2;
    for(char  c : str){
        if(c >= '0' && c <= '9'){
            s1 = s1 + c;
        }else{
            if(!s1.empty()){
                s2.push_back(s1);
                s1 = "";
            }
        }
    }
    if(!s1.empty()){
        s2.push_back(s1);
    }
    int maxLength = 0;
    for( string numstr1: s2){
        if(numstr1.length() > maxLength){
            maxLength = numstr1.length();
        }
    }
    string result;
    for(string numstr2 :s2){
        if(numstr2.length() == maxLength){
            result += numstr2;
        }
    }
    cout << result << "," << maxLength<<endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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