Why this c++ code failed to pass "ynyo" case?


  • 0
    D

    In VC environment, it returns 3. But it returns 2 on leetcode.

    int lengthOfLongestSubstring(string s) {
    int maxLen = 0;
    unordered_set<char> sub;
    unordered_set<char>::iterator iter;
    for(int i = 0; i < s.length(); i++)
    {
    char c = s.at(i);
    iter = sub.find(c);
    if(iter != sub.end())
    {
    int subLen = sub.size();
    if(subLen > maxLen)
    maxLen = subLen;
    sub.erase(sub.begin(), ++iter);
    }
    sub.insert(c);
    }
    if(sub.size() > maxLen)
    maxLen = sub.size();
    return maxLen;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.