It's my C++ answer.


  • 0
    S

    It's my C++ answer.
    '''
    class Solution {
    public:
    int lengthOfLongestSubstring(string s) {
    int i = 0, j = 0;
    int result = 0;
    unordered_map<char, int> iMap;
    for (j = 0; j < s.length(); j++) {
    if (iMap.find(s[j]) != iMap.end()) {
    if (iMap[s[j]] >= i) {
    i = iMap[s[j]] + 1;
    }
    iMap[s[j]] = j;

            }            
            else {
                iMap.insert({s[j], j});                    
            }            
            result = (result > (j - i + 1))? result: (j - i + 1);    
            
        }
        return result;
    }
    

    };
    '''


Log in to reply
 

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