思路:
搞一个hash表,统计一下每个字母出现多少次,然后再从前先后扫描一遍,
扫描到第一个只出现一次的字母,把这个字母返回就可以了。
C++里面的hash表:unordered_map<>
class Solution { public: char firstNotRepeatingChar(string s) { char res = '#'; if(!s.size()) return res; unordered_map<char, int> mmp; for(int i = 0; i < s.size(); i++) { mmp[s[i]] += 1; } for(int i = 0; i < s.size(); i++){ if(mmp[s[i]] == 1) return s[i]; } return res; } };
另一种写法:
class Solution { public: char firstNotRepeatingChar(string s) { unordered_map<char,int> count; for(auto c:s) count[c]++; char res='#'; for(auto c:s){ if(count[c] == 1) { res = c; break; } } return res; } };
原文链接: https://www.cnblogs.com/make-big-money/p/12330786.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/330500
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!