遍历字符串A,如果集合中不存在字符,将字符插入集合中;否则返回字符
class FirstRepeat {
public:
char findFirstRepeat(string A, int n) {
unordered_set<char> st;
for (const auto& ch : A) {
if (st.count(ch)) {
return ch;
} else {
st.insert(ch);
}
}
return ' ';
}
};
时间复杂度:O(n),用于遍历字符串A
空间复杂度:O(n),用于存储集合

京公网安备 11010502036488号