【去哪兒】首個重複字元
阿新 • • 發佈:2018-12-14
題目描述
對於一個字串,請設計一個高效演算法,找到第一次重複出現的字元。
給定一個字串(不一定全為字母)A及它的長度n。請返回第一個重複出現的字元。保證字串中有重複字元,字串的長度小於等於500。
測試樣例:
"qywyer23tdd",11
返回:y
#include <unordered_map> class FirstRepeat { public: char findFirstRepeat(string A, int n) { // write code here unordered_map<char,bool> table; char ans; for(auto item:A) { if(table.find(item)==table.end()) table[item]=1; else { ans = item; break; } } return ans; } };