剑指Offer_50_第一个只出现一次的字符
廖家龙 用心听,不照做

题目描述:

1
2
3
4
5
6
7
8
9
10
11
在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。

示例:

s = "abaccdeff"
返回 "b"

s = ""
返回 " "

限制:0 <= s 的长度 <= 50000

解法1:哈希表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
char firstUniqChar(string s) {

unordered_map<int, int> map;

for (auto ch : s) ++map[ch];

for (int i = 0; i < s.size(); ++i) {

if (map[s[i]] == 1) return s[i];

}

return ' ';
}
};