class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char,int> hashmap;
int max=0;
unordered_map<char, int>::iterator it;
for (int i = 0; i < s.size(); i++)
{
it = hashmap.find(s[i]);
if (it == hashmap.end())
{
hashmap[s[i]] = i;
}
else {
max = max < hashmap.size() ? hashmap.size() : max;
it++;
hashmap.erase(hashmap.begin(), it);
hashmap[s[i]] = i;
}
max = max < hashmap.size() ? hashmap.size():max;
}
return max;
}
};
同一个示例,vs跑出来的结果是对的,但上传力扣是错的。