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

提取不重复的整数

https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1?tpId=37&tags=&title=&difficulty=0&judgeStatus=0&rp=1&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D2%26tpId%3D37%26type%3D37

/*
思路:利用set容器来去重
    1. 利用string 获得输入的字符串
    2. 利用 reverse 翻转字符串
    3. 创建 set<char> 容器
    4. 创建结果字符串 res
    5. 遍历字符串,同时检查 遍历到的字符是否在set容器中,
        当当前字符不在set容器中时, 把当前字符用 append 函数加到res字符串中,同时把字符 insert 到 set 容器中
        当当前字符在set容器中时,跳过
    6. 打印res字符串 
*/
#include <algorithm>
#include <iostream>
#include <set>
#include <unordered_set>
using namespace std;

int main() {
    string str, res;
    cin >> str;
    reverse(str.begin(), str.end());

    set<char> mySet;

    for(auto c : str){
        if(mySet.count(c) == 0){
            res.append(1,c);
            mySet.insert(c);
        }
    }

    cout << res << endl;

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

全部评论

相关推荐

05-12 16:04
已编辑
江西财经大学 Java
点赞 评论 收藏
分享
想申请延毕了,找工作找到崩溃,越找就越想摆烂,还有25届的和我一样感受吗?
码农索隆:没事哒,好兄弟,慢慢来,调整心态,车到山前必有路,感到迷茫的时候,多抬头看看
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务