题解 | 提取不重复的整数

提取不重复的整数

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

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

int main() {
   
string str;
cin>>str;
int len=str.length();

set <char> nums;

for(int i=len-1;i>=0;i--){
    if(nums.find(str[i])!=nums.end()){ 
        // str[i]不等于nums.end()说明,说明str[i]在num中,找到了相同的数字
        // 因为set集合没有找到了就是返回nums.end()。
        continue;
    }else{
       cout<<str[i];
        nums.insert(str[i]);
    }
}

//解题思路:
//1. 从后往前遍历,
//2. 遍历到的每一个字符 拿去与集合num比较,如果在nunm中找到了同样的字符,则继续遍历str。
//   否则就把str[i]加入到nums中
//3. 这样输出 nums for (char c : nums) cout << c << " ";
//            cout << endl;
}

// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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