【劍指offer】面試題50.第一個只出現一次的字符

解題思路

先統計出每個字符的出現次數
再按照s中字符的順序進行遍歷

代碼

class Solution {
public:
	char firstUniqChar(string s) {
		char res=' ';
		map<char, int> hashMap;
		for (auto it=s.begin();it!=s.end();it++)
		{
			++hashMap[*it];
		}
		for (auto it = s.begin(); it != s.end(); it++)
		{
			if (hashMap[*it] == 1) return *it;
		}
		return res;
	}
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章