leetcode刷题50. 第一个只出现一次的字符

题目描述:在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。

示例:

s = "abaccdeff"
返回 "b"

s = "" 
返回 " "

限制:

0 <= s 的长度 <= 50000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路:使用hashmap

class Solution {
public:
    char firstUniqChar(string s) {
        unordered_map<char,int> hashmap;
        for(int i=0;i<s.length();i++){
            hashmap[s[i]]++;
        }
        for(int i=0;i<s.length();i++){
            if(hashmap[s[i]]==1)
                return s[i];
        }
        return ' ';
    }
};
发布了29 篇原创文章 · 获赞 0 · 访问量 499

猜你喜欢

转载自blog.csdn.net/weixin_43022263/article/details/104347155