剑指offer 50:第一个只出现一次的字符

在这里插入图片描述

思路
遍历两次数组,第一次用hashtable存储出现次数,第二次找出value值为1的第一个数。对于字母类问题hashtable处理,使用数组作为hashtable其实效率更高。在这里我直接用hashtable了。

class Solution {
    
    
public:
    char firstUniqChar(string s) {
    
    
        unordered_map<char, int> map;
        for (auto c : s) {
    
    
            map[c]++;
        }
        for (auto c : s) {
    
    
            if (map[c] == 1) return c;
        }
        return ' ';
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_44537258/article/details/114012820