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

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

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];
        
        
    }
}

全部评论

相关推荐

野猪不是猪🐗:我assume that你must技术aspect是solid的,temperament也挺good的,however面试不太serious,generally会feel style上不够sharp
面试吐槽bot
点赞 评论 收藏
分享
见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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