题解 | #找出字符串中第一个只出现一次的字符#

找出字符串中第一个只出现一次的字符

https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4

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

int main() {
    string s;
    cin >> s;
    unordered_map<char, int> map;
    for(int i = 0; i < s.size(); ++i){
        auto it = map.find(s[i]);
        if(it == map.end()){
            map[s[i]] = 1; 
        }else{
            map[s[i]]++;
        }
    }
    vector<char> res;
    for(auto &i : map){
        if(i.second == 1){
            res.push_back(i.first);
        }   
    }
    if(res.size() == 0){
        cout << "-1" << endl;
    }else if(res.size() == 1){
        cout << res[0] << endl;
    }else{
        int index = INT_MAX;
        for(int i = 0; i < res.size(); ++i){
            int it = s.find(res[i]);
            index = min(index, it);
        }
        cout << s[index] << endl;
    }

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

全部评论

相关推荐

小浪_Coding:1. 个人技能排版太乱, 写的技术栈太浅了, 跟测试,自动化相关的太少; 2. 项目开发类的太简单没有亮点, 算法类的项目建议只放一个,最好有自动化,CI/CD, pipline的项目, 需要更换; 3.整体排版需要优化, SOOB打招呼都需要注意等.
我的简历长这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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