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

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

https://www.nowcoder.com/practice/bd891093881d4ddf9e56e7cc8416562d

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

int main() {
    string str;
    getline(cin, str);
    int n = 0;
    int cut = 0;
    int prve = -1;

    for (int i = 0; i < str.size(); i++) {
        if (str[i] >= '0' && str[i] <= '9') {
            if (prve == -1) {
                prve = i;
            }
        } else {
            if (prve != -1) {
                if (i - prve > n) {
                    n = i - prve;
                    cut = prve;
                }
                prve = -1;
            }
        }
        if (i == str.size() - 1) {
            if (prve != -1) {
                if (i - prve > n) {
                    n = i - prve+1;
                    cut = prve;
                }

            }
        }

    }
   cout<<str.substr(cut,n)<<endl;

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

全部评论

相关推荐

牛客48784610...:深圳的变成录用进行中,这个是稳了吗,还没有收到邮件
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
04-23 10:48
点赞 评论 收藏
分享
996的工作制还是没能硬啃下去,快要面试怂了,取消了
牛客80700350...:很正常,不是所有人都能接受这种强度的。不叫怯战,这叫明智
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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