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

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

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

#include<iostream>
using namespace std;


int main()
{
    string s;
    
    while(cin>>s){
        int len=s.length();
        string res="";
        string t="";
        int i=0;
        int cnt=0;
        int ans=0;
        while(i<len){
            cnt=0;
            if(!isdigit(s[i])){
                i++;
                t="";
            }else{
                while(isdigit(s[i])&&i<len){
                    t+=s[i++];
                    cnt++;
                }
                if(cnt>ans){
                    res=t;
                    ans=cnt;
                    cnt=0;
                    t="";
                }else if(cnt==ans){
                    res+=t;
                    t="";
                }
            } 
        }
        cout<<res<<","<<ans<<endl;
    }

    return 0;
}

全部评论

相关推荐

09-12 18:28
门头沟学院 Java
网友描述的太精准了👍
迷茫的大四🐶:不管活脏还是累,钱到位就行,钱到位啥都不用抱怨
投递字节跳动等公司10个岗位
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-28 00:10
已编辑
码农索隆:这哥们库库在我帖子下评论
点赞 评论 收藏
分享
09-17 17:09
门头沟学院 Java
雨忄:有人给出过解法,拖晚点去,然后到时候再找其他理由商量,既增加他们的筛人成本,不一定会给你收回offer ,也能占位避免工贼
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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