#include <iostream>
#include <string>
#include <unordered_set>
#include <algorithm>
using namespace std;
int main() {
string n;
cin >> n;
reverse(n.begin(), n.end());
unordered_set<char> seen;
string result;
for (char digit : n) {
if (seen.find(digit) == seen.end()) {
result.push_back(digit);
seen.insert(digit);
}
}
cout << result << endl;
return 0;
}
- 确定输入类型:输入为正整数,大小在1~10^8,且要求将数字倒序,因此类型定为string。
- 将字符串倒置:用reverse()函数将输入序列倒置,具体来说,用两个迭代器限制倒置范围。
- 哈希集合存储:用哈希集合unordered_set存储数字,利用哈希集合查找迅速的优点。
- 遍历输入去重:利用哈希集合快速查找,通过对比确保每个数字只出现一次。
- 构建最终输出:每次有不重复的数字就用push_back()函数添加数字。