题解 | #首个重复字符# 集合简单使用
首个重复字符
https://www.nowcoder.com/practice/dab59997905b4459a42587fece8a75f4
遍历字符串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),用于存储集合
查看15道真题和解析