题解 | #提取不重复的整数#

提取不重复的整数

https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1

#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;

int main() {
    int n;
    cin>>n;
    string s = to_string(n);
    reverse(s.begin(),s.end());
    unordered_set<char> ch;
    vector<char> unique_ch;

    for(auto &c : s)
        if(ch.insert(c).second)//插入成功,second返回true,否则返回false, set用于去重
            unique_ch.push_back(c);//vector存储该元素,set基于哈希函数计算存储位置,会改变顺序
        else
            continue;
    for(auto &c : unique_ch)
        cout<<c;
    cout<<endl;
    return 0;
    
}

全部评论

相关推荐

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