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

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

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

hashmap可以把相同的key统计次数。

#include <algorithm>
#include <map>
#include <unordered_map>

using namespace std;

int main() {
    string str;
    while(cin>>str){
        unordered_map<char, int> m;
        for(int i = 0;i<str.size();i++){
            m[str[i]]++;
        }
        int pos=-1;
        for(int j = 0;j<str.size();j++){
            if(m[str[j]]==1) {pos=j;break;}
            
        }
        if(pos==-1) cout<<-1;
        else cout<<str[pos];
        
        
    }
}

全部评论

相关推荐

自由水:笑死了,敢这么面试不敢让别人说
点赞 评论 收藏
分享
05-09 12:23
已编辑
华南理工大学 Java
野猪不是猪🐗:给他装的,双九+有实习的能看的上这种厂我直接吃⑨✌们拿它练练面试愣是给他整出幻觉了
点赞 评论 收藏
分享
07-02 13:52
武汉大学 golang
骗你的不露头也秒
牛客87776816...:😃查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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