31.不同整数的计数问题(set+flag)【已解决】

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

int solution(string word) {
    // PLEASE DO NOT MODIFY THE FUNCTION SIGNATURE
    // write code here
    std::set<int> set = {}; 
    word.append("a");//最后添加任意的字母
    int temp = 0;
    int flag = 0;
    for(auto c: word){
        if(c >= '0' && c <= '9'){
            temp = temp * 10  + c - '0';
            flag = 1;
        }else{
            if(flag == 1){
                set.insert(temp);
                temp = 0;
                flag = 0;
            } 
        }
    }
    return set.size();
}

int main() {
    cout << (solution("a123bc34d8ef34") == 3) << endl;
    cout << (solution("t1234c23456") == 2) << endl;
    cout << (solution("a1b01c001d4") == 2) << endl;

    return 0;
}

set记录存在的数字种类,flag标志位说明是否是需要记录的数字

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值