lurenaa的博客

🥛set  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int lengthOfLongestSubstring(string s) {
set<char> st;
int mx = 0;
for(int i = 0; i < s.size();) {
if(st.count(s[i]) > 0) {
if(st.size()) {
st.erase(s[i - st.size()]);
}
} else {
st.insert(s[i]);
++i;
}
mx = max(mx, (int)st.size());
}
return mx;
}
};

Accepted

987/987 cases passed (48 ms)

Your runtime beats 25.3 % of cpp submissions

Your memory usage beats 14.41 % of cpp submissions (16 MB)